perform segue in viewDidLoad() without navigation controller in Swift
Asked Answered
C

3

7

I have three views (view 1 checks a server connection, view 2 shows the main content, view3 shows the support page) and I created them in the storyboard without coding. On starting my iOS app the view 1 shows a spinner while checking the server connection. If the connection check is passed then I want to go to view 2 and if it's failed then I want to go to view 3. The view 1 is only for the connection check and I don't want to go back to this view. So, I think I don't need a navigation controller, or?

In storyboard I connected all view with seques. In my view controller of view 1 I do this:

override func viewDidLoad() {
    super.viewDidLoad()

    let result:Bool = server.isServerAvailable(myURL)

    if (result == true) {
        performSegueWithIdentifier("ConnectionCheckToMain", sender: self)
    }
    else {
        performSegueWithIdentifier("ConnectionCheckToSupport", sender: self)
    }
}

But this segue in the viewDidLoad() function doesn't work, but I don't know why. I've added a button on view to check it. I've implemented the same code like in viewDidLoad() and it works fine. On clicking the button the next view loads.

Is there any idea why the code doesn't work?

Cymbal answered 29/9, 2014 at 12:42 Comment(0)
N
3

You can use this code for navigation.

    let vc : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ConnectionCheckToMain") as! UIViewController;
    self.presentViewController(vc, animated: true, completion: nil)
Nudibranch answered 29/9, 2014 at 13:36 Comment(4)
Thx, but now I get the following message: Warning: Attempt to present <HM.ViewControllerMain: 0x7ff488e2ba50> on <HM.ViewController: 0x7ff48b00e260> whose view is not in the window hierarchy!. Where's my mistake?Cymbal
I solved the problem, I migrated the code to viewDidAppear().Cymbal
I tried this solution but I get an error AnyObject is not convertible to 'UIViewController' did you mean to use 'as!' to force downcast? why?Mofette
'Cause Swift moved to 1.2, and now you have to force the downcast with the "as" alias. So : as! (1.2) = as (1.1).Dumbarton
S
6

This code solve for me (Swift 3) using "segue" (please replace "ConnectionCheckToMain" with your segue name.

DispatchQueue.main.async(execute: {
  self.performSegue(withIdentifier: "ConnectionCheckToMain", sender: nil)
  })
Sarre answered 6/1, 2017 at 15:44 Comment(0)
N
3

You can use this code for navigation.

    let vc : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ConnectionCheckToMain") as! UIViewController;
    self.presentViewController(vc, animated: true, completion: nil)
Nudibranch answered 29/9, 2014 at 13:36 Comment(4)
Thx, but now I get the following message: Warning: Attempt to present <HM.ViewControllerMain: 0x7ff488e2ba50> on <HM.ViewController: 0x7ff48b00e260> whose view is not in the window hierarchy!. Where's my mistake?Cymbal
I solved the problem, I migrated the code to viewDidAppear().Cymbal
I tried this solution but I get an error AnyObject is not convertible to 'UIViewController' did you mean to use 'as!' to force downcast? why?Mofette
'Cause Swift moved to 1.2, and now you have to force the downcast with the "as" alias. So : as! (1.2) = as (1.1).Dumbarton
A
1

I had the same problem and i solved it with this code:

dispatch_async(dispatch_get_main_queue(), { () -> Void in   
            let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewControllerClassId") as! ViewControllerClassName
            self.presentViewController(viewController, animated: true, completion: nil)
        })
Anaximenes answered 24/1, 2016 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.