iOS - How to push to a view controller and maintai

2019-09-04 05:29发布

I've got a single navigation controller that houses the primary view of the app (A dashboard of sorts) which then branches off to the other views in the app via segues etc..

There's a hamburger menu, which makes use of a Cocoa Pods sliding view controller. Basically, my question is is it possible to navigate to specific view controllers via the menu and maintain the correct "stack" going back. I'm aware you can just push to a single controller, but I'd like (if possible) to retain the view hierachy going back.

E.g.

There's a link in the menu to Page 1.3

I want to be able to push to Page 1.3

Then have the back button on that view to go to Page 1.2, then Page 1.1 etc..

I'm not sure whether this is standard practice in iOS apps, or whether it's better to adopt an Android esque approach and just add views to the stack, not worrying about trying to form the correct navigation structure when they go back through the views.

Hopefully all of that makes sense.

2条回答
We Are One
2楼-- · 2019-09-04 05:58

If you are happy to do it in code, rather than a segue, you can use the UINavigationController's setViewControllers:animated: method (see the documentation). You will need to instantiate the relevant VCs, initialise them with any relevant data/state, and then build an array with them in order from the root view controller to the top view controller:

NSArray *newStack = @[rootVC, ..., page11VC, page12VC, page13VC];
[self.navigationController setViewControllers:newStack animated:YES];

Then popping from page13VC will go to page12VC, etc, etc. You can get rootVC and any other pre-existing VCs using the viewControllers property of the navigation controller.

查看更多
姐就是有狂的资本
3楼-- · 2019-09-04 05:59

I meet the same problem in my project,here is my experience. If you just want to do simple jump, you can try the three basic navigation function:

[self.navigationController popToRootViewControllerAnimated:YES]
[self.navigationController popToViewController:[[self.navigationController.viewControllers count] - mypopcount] animated:YES];
[self.navigationController popViewControllerAnimated:YES];

Bind the action with the button, you can do that. But, if you want to do more flexible page jump in ios, you must consider: First of all, you need to confirm that there is no direct data interact between these view controllers, which means you need coreData or other sqlite tools to store data, so the destination page can load data itself. Secondly, you should consider the strategy, page jump means pop all viewcontroller and push a new one, or just pop or push a new one just on the navigation stack. Good Luck!

查看更多
登录 后发表回答