8

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?

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Carmen
  • 125
  • 1
  • 3
  • 9

3 Answers3

15

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)
giapnh
  • 2,950
  • 24
  • 20
2

You can change rootViewController of your application:

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

Dmitriy
  • 552
  • 1
  • 6
  • 20
0

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)
jaytrixz
  • 4,059
  • 7
  • 38
  • 57