1

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.

Lukesivi
  • 2,206
  • 4
  • 25
  • 43
  • I'm unclear - is the webView embedded in the tableViewCell, or is it displayed modally/pushed over the full tableViewController? – pbasdf Oct 18 '15 at 18:55
  • @pbasdf sorry, I don't understand the question... The webView is in it's own separate UIViewController in the MainStoryBoard and has its own class. I'm assuming that I should access it from the TableViewController class because that's where I'm tapping the cell which ultimately leads to the link associated with that cell. Hope that answered your question... – Lukesivi Oct 18 '15 at 18:57
  • Thanks, that helps... – pbasdf Oct 18 '15 at 19:08
  • Thank you. Just to know, I added some more code of another way I'm going about it... Thanks. – Lukesivi Oct 18 '15 at 19:09

1 Answers1

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

Storyboard image

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.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Thank you. I'm getting a crash at ` let URLRequest = NSURLRequest(URL: (self.url)!)' in the LinkViewViewController. Just a few questions: 1) i did this: 'myWebView.url = NSURL(string: linksArray[indexPath.row])' is that what you meant by `url`? 2. in the ViewDidLoad of the LinKViewController, should i just leave it as it? `let URLRequest = NSURLRequest(self.url)` ? Otherwise, as mentioned i get a `fatal error: unexpectedly found nil while unwrapping an Optional value` – Lukesivi Oct 18 '15 at 19:55
  • Both look good to me. Can you step through, or use print() to check which value is nil? – pbasdf Oct 18 '15 at 20:01
  • I'm unable to print the `URLRequest` nor the `url` variable. Stepping through isn't doing me any good either but it point me to `LinkViewViewController `. I'm assuming the issue is with var url: NSURL? because its the only optional, and it's also called in the URLRequest. Not sure what to do about that... I am already printing to the logs the links that I'm loading. So I do see them. Maybe I'm not converting them correctly from a String in Parse to a NSURL in Swift? – Lukesivi Oct 18 '15 at 20:10
  • Try using a test URL: `myWebView.url = NSURL(string:"http:...whatever...")` – pbasdf Oct 18 '15 at 20:19
  • Great idea... It didn't work. It had the same crash. – Lukesivi Oct 18 '15 at 20:22
  • Is the tableViewCell linked to the LinkViewViewController by a segue? – pbasdf Oct 18 '15 at 20:26
  • Yes. I also tried linking the viewController itself. I get a libstdc++ error. – Lukesivi Oct 18 '15 at 20:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92666/discussion-between-rinyfo4-and-pbasdf). – Lukesivi Oct 18 '15 at 20:27