Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接

Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController(Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接)

本文介绍了Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击 tableViewCell 时,我希望在带有 webView 的新 viewController 中打开一个链接(特定于该单元格的 indexPath.row).

When i tap a tableViewCell I want a link (that is specific to that cell's indexPath.row) to open up in a new viewController with a webView.

示例:我点击 tableView 中的第三个单元格,www.apple.com 在新的 viewController 上打开.

Example: I tap the 3rd cell in the tableView and www.apple.com opens up on a new viewController.

我正在尝试使用 didSelectRowAtIndexPath 来做到这一点,但我不知道如何将代码放在一起,以便 linksArray 中的代码(我有所有存储的链接)当我点击 tableViewCell

I'm trying to do that with didSelectRowAtIndexPath but I can't figure out how to put the code together so that the code from the linksArray (where i have all the links stored) works together when I tap the tableViewCell

我正在从 Parse.com 查询这些链接.这是我的数组:

I'm querying these links from Parse.com. This is my array:

var linksArray = [NSURL]()

我创建了一个名为 LinkViewViewController 的新类,其中我只将 webView 作为 @IBOutlet weak var webView: UIWebView! 连接,仅此而已.我取出了 viewDidLoad 方法,以便可以从具有单元格的 FeedTableViewController 类控制 LinkViewViewController.

I created a new Class called LinkViewViewController where I have just the webView connected as a @IBOutlet weak var webView: UIWebView! and nothing else. I took out the viewDidLoad method so that I can control the LinkViewViewController from the FeedTableViewController class that has the cells.

下面是我的代码

 override func tableView(tableView: UITableView,  didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let myWebView = tableView.dequeueReusableCellWithIdentifier("cell",    forIndexPath: indexPath) as! LinkViewViewController

    var url = NSURL(string:"(links[indexPath.row])")

    var request = NSURLRequest(URL: url!)

    myWebView.webView.loadRequest(links[indexPath.row] as! NSURLRequest)

}

更新

我也在尝试这样做,这可能更合乎逻辑.我正在访问 ProductInfo 类中存储在 Parse 中的链接.productName 数组表示单元格的组织顺序,以便在点击产品名称时可以访问链接.

I am also trying to do it this way, which maybe more logical. I am accessing the links that are stored in Parse in the ProductInfo class. The productName array represents in what order the cells are organized in so that the the links can be accessed when tapping on the product name.

   override func tableView(tableView: UITableView,  didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let myWebView = tableView.dequeueReusableCellWithIdentifier("cell",    forIndexPath: indexPath) as! LinkViewViewController
  //Assuming that myWebView is the right method/way to go about it. 

    var link = PFObject(className: "ProductInfo")

    link["pLink"] = productName[indexPath.row]


       /* Code missing: I'm accessing where the link is (the
        indexPath.row) associated with the productName. 
        I don't know how to go about this now... How do i access the 
        webView in the other viewController where the corresponding 
        link appears?

        */
}

我也尝试过创建从单元格到 webView 的 segue,但这也不起作用......

I've also tried creating a segue from the cell to the webView but that didn't work either...

如何设置 didSelectRowAtIndexPath 以触发链接出现在新视图中?

How can I set up the didSelectRowAtIndexPath so that a link is triggered to appear in a new view?

思考:info.plist中的NSTransportSecurity也有问题?

任何帮助都意味着很多.

Any helps means a lot.

谢谢.

推荐答案

你不应该使用 dequeueReusableCellWithIdentifier 来创建你的下一个视图控制器.您应该使用情节提要的 instantiateViewControllerWithIdentifier 来创建实例:在情节提要中为 viewController 设置情节提要 ID"

You should not be using dequeueReusableCellWithIdentifier to create your next view controller. You should use the storyboard's instantiateViewControllerWithIdentifier to create the instance: Set the "Storyboard ID" for the viewController in your storyboard

例如,如果将其设置为WebViewController",则可以使用以下命令创建实例:

If you set it to, for example, "WebViewController" you can then create the instance with:

let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as! LinkViewViewController

我的建议是给 LinkViewViewController 自己的 URL 属性,

My recommendation would be to give the LinkViewViewController its own URL property,

var url : NSURL?

并在创建实例后传递 URL:

and to pass the URL after creating the instance:

myWebView.url = url

然后展示新的 VC:

self.presentViewController(myWebView, animated: true, completion: nil)

在LinkViewViewController的viewDidLoad中,将URL加载到webView中:

In the viewDidLoad of LinkViewViewController, load the URL into the webView:

let URLRequest = NSURLRequest(self.url)
self.webView.loadRequest(URLRequest)

(未经测试输入;请原谅任何拼写错误/可选选项等错误).

(Typed without testing; please forgive any typos/errors with optionals etc).

最后一点,此方法使用 didSelectRow 方法来创建视图控制器实例并传递 URL.另一种选择是通过 ctrl 从原型单元拖动到视图控制器来使用 segue.然后你需要在 prepareForSegue 方法中实现类似的代码来设置 url.

As a final note, this method uses the didSelectRow method to create the view controller instance and pass the URL. Another option would be to use a segue by ctrl-dragging from the prototype cell to the view controller. You then need to implement similar code in prepareForSegue method to set the url.

这篇关于Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接

基础教程推荐