1

I have two viewcontroller. I am trying to load multiple url in one webview using buttons. My first viewcontroller contains buttons and second viewcontroller contains a uiwebview. I have connected two buttons to the webview by control+click+dragging.Here is my code. It doesn't work. What's the problem?

first vc:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var link1: UIButton!

    @IBOutlet var link2: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func link1(sender: AnyObject) {
        self.performSegueWithIdentifier("link1", sender: sender)
    }

    @IBAction func link2(sender: AnyObject) {
        self.performSegueWithIdentifier("link2", sender: sender)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.

        let websiteController = segue.destinationViewController as webViewController
        if segue.identifier == "link1" {
            websiteController.urlWebsite = "www.google.com"
        } else if segue.identifier == "link2" {
            websiteController.urlWebsite = "http://www.stackoverflow.com"
        }

    }



}

second vc:

import UIKit

class webViewController: UIViewController {

    @IBOutlet var webView: UIWebView!

    var urlWebsite: String?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let myURLString = urlWebsite
        let myURL = NSURL(string: myURLString!)
        let myURLRequest = NSURLRequest(URL: myURL!)
        webView.loadRequest(myURLRequest)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
tuhin
  • 11
  • 5
  • 1
    What does not work? The button events or the segue? Do you reach the `prepareForSegue`function? – Rashwan L Mar 28 '16 at 08:36
  • When i click on button "link1" it is crashing and showing message - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'link1''. Same in "link2". – tuhin Mar 28 '16 at 09:40
  • You can create segue like these screen shot image urls: http://postimg.org/image/7pvin00l1/ http://postimg.org/image/qit25ylqp/ – dev_binod Mar 28 '16 at 09:53
  • This time not crashing but after clicking button no response in second vc – tuhin Mar 28 '16 at 10:19
  • What is your xCode version? – dev_binod Mar 28 '16 at 19:52

1 Answers1

0

I think, your issue is in this line of code :

if segue.identifier == "link1" {
    websiteController.urlWebsite = "www.google.com"
}

you need to add prefix http:// in url . so the your code is like below

if segue.identifier == "link1" {
    websiteController.urlWebsite = "http://www.google.com"
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
dev_binod
  • 427
  • 2
  • 9