让我们总结一下。
您在UINavigationController 中嵌入了UIWebView。
您要做的是在导航栏中添加一个按钮,以便用户可以存储他/她想要跟踪的一些网站的 URL。如果用户按下此按钮,则当前网站的 URL 将存储在您的应用程序中,并显示在您称为 FavoriteView 的 UITableView 上。
我假设您的 FavoriteView 和您的 Web 视图由同一个控制器管理如果不是这样,请根据您的结构更改代码。
如果你只需要存储URL,那么你可以使用NSUserDefaults,它比Core Data更容易操作(因为你似乎是从iOS开发开始的,这样开始可能会更好)。
你的控制器应该是这样的:
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)中显示存储的URL。管理您的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
}
最后,当你选择你最喜欢的视图的单元格时,你想加载所选的 URL,所以实现这个UITableViewDelegate 的方法:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let url = self.urlArray[indexPath.row]
let request = NSURLRequest(URL: url)
self.webView.loadRequest(request)
}
我再说一遍:你必须适应你的代码,我猜你的UIWebView 和你的UITableView 不是由同一个控制器管理的。