2

I have a Navigation controller that manages through a segue two web views. When someone clicks a specific link in the first view controller, the link is opened in the second view controller in another web view. Therefore the user clicks a link, a segue is performed to the next view and the new page is loaded.

I would like for the segue to wait until the webpage in the second view controller has loaded in the background before performing the segue.

I tried to do something like this (as the loadURL request in in the viewDidLoad function) but (as I expected) I get an error (fatal error: Can't unwrap Optional.None) as the webView has not yet loaded! Anyone knows how to prevent this?

//code 
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if(segue.identifier == "second"){         
        segue.destinationViewController.load()
        segue.destinationViewController.viewDidLoad()            
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tosen
  • 76
  • 1
  • 5

1 Answers1

0

I would try this:

  • create a dummy webview in the first view controller, (a webview not added to the main view).
  • in the same first VC, when the user clicks the link, begin loadRequest on the dummy webview
  • then in webViewDidFinishLoad (of first VC) call performSegue
  • in prepareForSegue add the dummy webView to the second view controller main view (using addSubView)

I noted that webViewDidFinishLoad is called multiple times and you have to add some code to really know when the loading is finished (and not to call prepareForSegue multiple times).

It's not very simple but hope it helped.

EDIT:

This is a (raw) example of the code, I have tested it and it works, but you have to adjust something (as the dimensions of the view and the finishLoad thing) (the action of the button is openPage):

class DetailViewController: UIViewController, UIWebViewDelegate {

    var dummyWebViewInFirstVC: UIWebView = UIWebView(frame: CGRectMake(0, 0, 320, 480))
    var done = false

    @IBAction func openPage(sender: AnyObject) {


        let url = NSURL(string: "http://www.apple.com/")
        let request = NSURLRequest(URL: url)
        dummyWebViewInFirstVC.loadRequest(request)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        dummyWebViewInFirstVC.delegate = self
    }

    func webViewDidFinishLoad(theWebView: UIWebView) {

        if !done {
            self.performSegueWithIdentifier("openPage", sender: self)
            done = true;
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

        let secondViewController : UIViewController = segue.destinationViewController as UIViewController
        secondViewController.view.addSubview(dummyWebViewInFirstVC)
    }
}
Community
  • 1
  • 1
valfer
  • 3,545
  • 2
  • 19
  • 24
  • But using let NextView = secondViewController() NextView.secondWebView = tempWebView Doesn't work.. is there a way of passing a loaded webView to another UIView? – Tosen Jul 31 '14 at 08:27
  • Use: secondViewController.view.addSubview(dummyWebViewOfFirstVC) – valfer Jul 31 '14 at 08:51
  • I get fatal error: Can't unwrap Optional.None by doing that.. (as the other view is of course not loaded! Is there a way to prevent this?) – Tosen Jul 31 '14 at 10:13