How to reset / restart viewcontroller in Swift?

I want to have a “Reset” button on the navigation bar, and I would like it to be connected to IBAction to sort the “restart” controller.

I have some segments from another controller that changes some aspects of the viewcontroller (which has a collectionview ), and I want the user to be able to start all over again. Does anyone have any suggestions on how to proceed?

+6
source share
5 answers

If you have integrated the navigation controller in the view controller, 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) 
+3
source

Why not remove and submit the view controller again? Or, perhaps transfer the logical value from the view controller to the view presentation controller (the one that opens the collection view controller), find it in sight and display the view controller again (with the collection view). You can set an animated flag (false) when you do this, so it seems like you have reset everything.

0
source

Here's how I do it in Swift 2.1 with my Home view inside the 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
source

You can change the rootViewController of your application:

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

0
source

try it

 func refreshView() { // Calling the viewDidLoad and viewWillAppear methods to "refresh" the VC and run through the code within the methods themselves self.viewDidLoad() self.viewWillAppear() } 

Then you can make the button and attach the @IBAction method to it using the refreshView method refreshView this

 @IBAction func refreshVC(sender: AnyObject) { self.refreshView() } 
-2
source

Source: https://habr.com/ru/post/988487/


All Articles