how to pop a controller off the navigation stack without using the navigation bar

非 Y 不嫁゛ 提交于 2020-01-03 03:47:21

问题


I'm trying to implement a navigation controller with some hierarchical views. I want to use a regular UIViewController to present choices for drilling down, I don't want to use the navigation bar - I want to have my own, custom buttons for returning back up a level.

I see examples like:

[[self navigationController] pushViewController:nextViewController animated:YES];

and my questions are these: Is navigationController a property of all UIViewControllers? Can I refer to self.navigationController regardless of the view that's on the stack? If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewController animated:YES];

Each view I present will need a button to return to the previous view, or to the root view, depending on the situation. I want to create that button in each view controller and control which view in the stack it returns to. Am I on the right track?


回答1:


Is navigationController a property of all UIViewControllers?

Yes.

Can I refer to self.navigationController regardless of the view that's on the stack?

Every UIViewController on the UINavigationController's stack will return the UINavigationController object when calling navigationController on it.

If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewControllerAnimated:YES];

Yes. popToRootViewControllerAnimated: will take the user to the root UIViewController for the UINavigationController, and you can use [self.navigationController popViewControllerAnimated:YES]; to just pop off the top UIViewController. This last one does the same as tapping the Back UIBarButtonItem.

Am I on the right track?

Yes :)



来源:https://stackoverflow.com/questions/3249240/how-to-pop-a-controller-off-the-navigation-stack-without-using-the-navigation-ba

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