How to reset/restart viewcontroller in Swift?

后端 未结 3 1047
忘了有多久
忘了有多久 2021-01-05 00:50

I want to have a button on my navigation bar \"Reset\" and I would like this to be connected to an IBAction to sort of \"restart\" the controller.

I ha

相关标签:
3条回答
  • 2021-01-05 00:56

    If you embed navigation controller to your view controller then you can use this code:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController")
    let viewcontrollers = self.navigationController.viewControllers
    viewcontrollers.removeLast()
    viewcontrollers.append(vc)
    self.navigationControllers?.setViewControllers(viewcontrollers, animate: true)
    
    0 讨论(0)
  • 2021-01-05 00:57

    This is how I do it in Swift 2.1 with my Home view inside a UINavigationController:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let homeVC = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
    self.navigationController?.presentViewController(homeVC, animated: false, completion: nil)
    self.navigationController?.dismissViewControllerAnimated(false, completion: nil)
    
    0 讨论(0)
  • 2021-01-05 01:00

    You can change rootViewController of your application:

    UIApplication.shared.keyWindow?.rootViewController = UIViewController()

    0 讨论(0)
提交回复
热议问题