我正在使用swift在iOS中构建一个读者动漫应用。
我使用UIWebView显示链接到人们想要阅读的动漫的URL。 我有一个FavoriteView(使用TableView),显示人们在TableViewCell中最喜欢的动画。 当人们在UIWebView中阅读时,我想在导航栏上创建一个“Like Button”。
其中,人们可以按下“Like Button”,我想做点什么来保存点击事件(或者在WebView中显示的URL,我不确定)将它像对象一样存储(我可以这样做吗?:(),然后我想把它传递给FavoriteView,将它显示在FavoriteView的Cell中。
有人建议我用coredata存储点击事件(如对象),但我无法想象它是什么。
答案 0 :(得分:0)
总结一下。
UIWebView
中嵌入了UINavigationController
。
你想要做的是在导航栏中添加一个按钮,这样用户就可以存储他/她想要跟踪的一些网站的URL。如果用户按下此按钮,则当前网站的网址会存储在您的应用中,并显示在您呼叫UITableView
的{{1}}上。
我假设您的FavoriteView
和您的网络视图由同一个控制器管理如果不是这样,那么请相应地将代码更改为结构
如果您只需要存储网址,那么您可以使用比FavoriteView
更容易操作的NSUserDefaults
(因为您似乎从iOS开发开始,以这种方式开始可能更好)。
你的控制器应该是这样的:
Core Data
您必须将以下代码添加到class ViewController: UITableViewDelegate, UITableViewDataSource
{
let favoriteView = UITableView()
let urlArray = Array<NSURL>()
let webView = UIWebView()
override func viewDidLoad() {
super.viewDidLoad()
// Initialise your views
}
// Etc... ( rest of your code )
}
(除非您的代码结构不同,否则您应该根据当前配置调整答案)。
首先,向ViewController
添加一个按钮:
UINavigationBar
然后,您可以实现点击按钮时将调用的let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(addButtonTapped))
self.navigationItem.rightBarButtonItem = addButton
方法:
addButtonTapped
最后,您可以在func addButtonTapped()
{
// We first check that the webView actually has a URL, otherwise we return
guard let url = self.webView.request?.URL
else {return}
// Then we store this URL is NSUserDefaults
// As we will have other URLs to store, it's interesting to store an array of URLs in NSUserDefaults
let defaults = NSUserDefaults.standardUserDefaults()
// We first check if the array already exists in NSUserDefaults
// If it is the case, we use the existing one, if not, we use a new one
if let existingUrlArray = defaults.objectForKey("keyUrlArray") as? Array<NSURL>
{
self.urlArray = existingUrlArray
}
self.urlArray.append(url)
defaults.setObject(self.urlArray, forKey: "keyUrlArray")
self.favoriteView.reloadData
}
FavoriteView
中显示存储的网址。管理UITableView
的控制器必须以这种方式实现FavoriteView
方法:
UITableViewDelegate
最后,您希望在选择自己喜欢的视图的单元格时加载所选的网址,因此请执行此// Call this method in viewDidLoad() so your UITableView is initialised
func initialiseFavoriteView()
{
self.favoriteView.delegate = self
self.favoriteView.dataSource = self
self.favoriteView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier")
self.loadFavoritesAndUpdateTableView()
}
// This method fetches the favorites url from NSUserDefaults and store them into self.urlArray which contains the data of your UITableView
func loadFavoritesAndUpdateTableView()
{
if let urlArrayCheck = NSUserDefaults.standardUserDefaults().objectForKey("keyUrlArray") as? Array<NSURL>
{
self.urlArray = urlArrayCheck
}
self.favoriteView.reloadData()
}
// MARK: UITableViewDelegate methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
let url = self.urlArray[indexPath.row]
cell.textLabel?.text = url.absoluteString
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.urlArray.count
}
方法:
UITableViewDelegate
我再重复一次:您必须将其改编为您的代码,我猜您的func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let url = self.urlArray[indexPath.row]
let request = NSURLRequest(URL: url)
self.webView.loadRequest(request)
}
和UIWebView
不会由同一个控制器管理。
答案 1 :(得分:0)
更容易做到这一点:
var mvcUrlPartsDict = new Dictionary<string, string>();
var routeValues = HttpContext.Request.RequestContext.RouteData.Values;
if (routeValues.ContainsKey("controller"))
{
if (!mvcUrlPartsDict.ContainsKey("controller"))
{
mvcUrlPartsDict.Add("controller", string.IsNullOrEmpty(routeValues["controller"].ToString()) ? string.Empty : routeValues["controller"].ToString());
}
}
if (routeValues.ContainsKey("action"))
{
if (!mvcUrlPartsDict.ContainsKey("action"))
{
mvcUrlPartsDict.Add("action", string.IsNullOrEmpty(routeValues["action"].ToString()) ? string.Empty : routeValues["action"].ToString());
}
}
if (routeValues.ContainsKey("id"))
{
if (!mvcUrlPartsDict.ContainsKey("id"))
{
mvcUrlPartsDict.Add("id", string.IsNullOrEmpty(routeValues["id"].ToString()) ? string.Empty : routeValues["id"].ToString());
}
}
var uri = string.Format("/{0}/{1}/", mvcUrlPartsDict["controller"], mvcUrlPartsDict["action"]);
Debug.WriteLine(uri);
然后创建一个名为 <activity
android:name=".FirstActivity"
android:noHistory="true">
// intent filters and other crap
</activity>
的数组。
然后:
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(addFavorite))
self.navigationItem.rightBarButtonItem = addButton
保存:
favorites
获得:
func addFavorite() {
favorites.append(theFavorite)
}