Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController

痴心易碎 提交于 2019-12-01 10:40:53

问题


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.

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

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

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

var linksArray = [NSURL]()

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.

Here's my code below

 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)

}

UPDATE

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?

        */
}

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

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

Thought: NSTransportSecurity in the info.plist also an issue?

Any helps means a lot.

Thanks.


回答1:


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

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

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

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

var url : NSURL?

and to pass the URL after creating the instance:

myWebView.url = url

and then present the new VC:

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

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).

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.



来源:https://stackoverflow.com/questions/33201437/swift-triggering-tableviewcell-to-lead-to-a-link-in-a-uiwebview-in-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!