Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset/restart viewcontroller in Swift?

Tags:

swift

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 have some segues from another controller that changes some aspects of the viewcontroller (that has a collectionview) and I want the user to be able to start over. Does anyone have any suggestions as to how to proceed?

like image 477
Carmen Avatar asked Jun 04 '15 01:06

Carmen


2 Answers

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)
like image 162
giapnh Avatar answered Oct 20 '22 01:10

giapnh


You can change rootViewController of your application:

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

like image 24
Dmitriy Avatar answered Oct 20 '22 00:10

Dmitriy