【问题标题】:How to make a like button for UIWebView?如何为 UIWebView 制作一个赞按钮?
【发布时间】:2016-07-17 20:17:13
【问题描述】:

我正在使用 swift 在 iOS 中构建一个阅读动漫应用程序。

我使用 UIWebView 显示链接到人们想要阅读的动漫的 URL。 我有一个 FavoriteView(使用 TableView),它显示人们在 TableViewCell 中最喜欢的动漫。 当人们在 UIWebView 中阅读时,我想在导航栏上制作一个“Like Button”。

在其中,人们可以按下“Like Button”,我想做一些事情来保存该点击事件(或 WebView 中显示的 URL,我不确定)以将其存储为对象(我可以这样做吗?:(),然后我想将它传递给FavoriteView以显示在FavoriteView的Cell中。

有人建议我通过 coredata 存储该点击事件(如对象),但我无法想象它是什么。

【问题讨论】:

  • 我不明白:您是要存储点击事件还是要存储网站的 url?我的意思是,您的表格视图上将显示什么?
  • 感谢您的回复。我想我确实想存储网站的 url,然后我想将该 URL(在 WebView 中)传递给 HistoryView 的单元格以显示人们过去阅读的内容,如果可以,请帮助我,谢谢

标签: ios swift button core-data uiwebview


【解决方案1】:

让我们总结一下。

您在UINavigationController 中嵌入了UIWebView。 您要做的是在导航栏中添加一个按钮,以便用户可以存储他/她想要跟踪的一些网站的 URL。如果用户按下此按钮,则当前网站的 URL 将存储在您的应用程序中,并显示在您称为 FavoriteViewUITableView 上。

我假设您的 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 不是由同一个控制器管理的。

【讨论】:

  • 我想告诉你,我正在部署你的想法,而且看起来很成功,非常感谢你帮助我
  • 我不使用情节提要。我使用 .xib 文件。我的 UIWebView 和 UITableView 由两个不同的控制器管理,所以我仍然找不到方法:将存储在 UIWebView 中的数据(那些 URL)传递给 UITableView。我想了很多,但我仍然不明白该怎么做。有空的时候请帮帮我。谢谢你
  • 好吧,在管理您的UITableView 的控制器中有一个名为favoritesUrls 的数组。在此控制器的viewDidLoad 方法中,从NSUserDefaults 获取数据:self.favoritesUrls = NSUserDefaults.standardUserDefaults.objectForKey("keyUrlArray"),并将此数组用作表格视图的数据源(所以基本上,将urlArray 替换为favoritesUrls 是@987654350 @我在代码中指明的方法
【解决方案2】:

更简单的方法:

let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(addFavorite))
self.navigationItem.rightBarButtonItem = addButton

然后创建一个名为favorites的数组。

然后:

func addFavorite() {
    favorites.append(theFavorite)
}

保存:

NSUserDefaults.standardUserDefaults().setObject(favorites, forKey: "favorites")

获取:

if let f = NSUserDefaults.standardUserDefaults().objectForKey("favorties") {
    favorites = f as! [\*The type of favorites e.g. String or Int*\]
}

【讨论】:

  • 非常感谢您阅读该主题并帮助我。我会试试你的方法。谢谢你!!!!!!!!!!
猜你喜欢
  • 2021-07-19
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多