1

I was trying to build a app. which have many page and every page have a navigation item called Home.

when it click it will go home page, mean's start page. I don't want to instantiateViewController. I want fist initialise page will come. how I can do this. I don't want to dismiss page. because it's take many click to go home page

I was try this

self.dismiss(animated: true, completion: {});  
self.navigationController?.popViewController(animated: true);

initial view controller

3 Answers3

2
self.navigationController?.popToRootViewController(animated: true)

Pops all the view controllers on the stack except the root view controller and updates the display.

Daniyal Raza
  • 352
  • 3
  • 13
2

Swift 3+: You can relaunch app by using below line of code.

func moveToRootViewController(){
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let window = UIApplication.shared.keyWindow
    window?.rootViewController = storyboard.instantiateInitialViewController()
}
Shiv Kumar
  • 932
  • 7
  • 12
0

You can use UINavigationController method popToRootViewController(animated:) for your purpose. Set your Home as root of your Navigation Controller. You can push required View Controllers using same Navigation Controller. Whenever you press Home button inside any page just pop all View Controller and display Root View Controller using above method. You can use it as

self.navigationController?.popToRootViewController(animated: true)

call this method on home button click in every pageof app.

From Apple documentation :

popToRootViewController(animated:) Pops all the view controllers on the stack except the root view controller and updates the display.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19