perform segue in viewDidLoad() without navigation controller in Swift

扶醉桌前 提交于 2019-12-08 18:03:41

问题


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?


回答1:


You can use this code for navigation.

    let vc : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ConnectionCheckToMain") as! UIViewController;
    self.presentViewController(vc, animated: true, completion: nil)



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/26100220/perform-segue-in-viewdidload-without-navigation-controller-in-swift

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