assigning a custom destination to popToRootViewController swift 4

亡梦爱人 提交于 2021-01-29 10:37:47

问题


I am using navigationController?.popToRootViewController(animated: true) to dismiss my current view to the previous view. My view controller relationship looks like this.

VC1->VC2
VC1->VC3
VC3->VC2

Whenever the client is in VC2 I want to pop the navigation controller back to VC1. This works fine when the rootviewcontroller is set to VC1. However, when the client uses a segue from VC3 to enter VC2, the rootviewcontroller is set to VC3 and the navigation controller pops to VC3.

I tried to change the rootviewcontroller like this.

 // set root view controller
    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let VC1 = mainStoryboard.instantiateViewController(withIdentifier: "VC1") as! FirstViewController
    appdelegate.window!.rootViewController = VC1
    navigationController?.popToRootViewController(animated: true)

But this actually returns the viewcontroller to the root view controller (VC1) even before the line "navigationController?.popToRootViewController(animated: true)" is executed so there is no animation.

Is there any way to set the rootviewcontroller of a navigation controller without presenting the root view controller right away?


回答1:


If you put appdelegate.window!.rootViewController = VC1, the stack of controllers has died, you only have in the stack an alone Controller, therefore you can't apply popToRootViewController.

If this is your required navigation, maybe, this post helps you:



来源:https://stackoverflow.com/questions/51093301/assigning-a-custom-destination-to-poptorootviewcontroller-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!