简体   繁体   中英

iOS: how to resolve issue with Navigation Controller scene in big Storyboard

I am very new in the iOS programming and some simple things confuse me a lot.

My application contains 50 scenes each of which use navigation bar with bar button to support in-app navigation. Near 30 of them are presented modally.

Am I correct that it means that I should add more than 30 separate Navigation Controller to my Storyboard?

If so - it will makes my Storyboard very difficult to read and support.

Please tell me - what is the best way to act in such situation? I have tried next:

  1. I tried to find setting like "do not show Navigation Controller" without success.

  2. I have found the way to resize each Navigation Controller according to this post

  3. I can do the "trick"

    3.1 add Navigation Controller

    3.2 remove Navigation Controller

    3.3 after that I still have "Navigation item" embedded and could configure it programmatically

May be there is less "tricky" way to do it?

If you have to customize the navigation bar, both doing it in the interface builder or in code will be difficult to managing.

If I were you, and If the buttons on the navigation bar is simple, I would put them in the code.

For example in your storyboard you make view controllers without navigation controller wrapping them.

In your code when you present view controllers modally init them with a storyboard segues and init a uinavigationcontroller and set topviewcontroller to the presenting view controller.

And by using presentViewController in the view controller class you can modally present a view controller.

before you present don't forget to add the buttons.

Here is my example

var viewControllerToShowModally = storyboard?.instantiateViewControllerWithIdentifier("VIEWCONTROLLER") as!YOUR_CUSTOM_VIEWCONTROLLER
var navigationController = UINavigationController(rootViewController: viewControllerToShowModally)

navigationController.navigationItem.title = "Title"
// Or add buttons, I mean customize the navigation bar!

presentViewController(navigationController, animated: true, completion: nil)

and in your modally presented view controller call the following method to dismiss

dismissViewControllerAnimated(true, completion: nil)

In this way in your storyboard you don't need to connect the 30 view controllers with the messy segue lines. Also there won't be 30 navigation controllers.

When you present something on the screen, you start a new flow. Every navigation controller represents a flow so if you present 30 view controllers, you should have 30 navigation controllers.

For example if you have a flow like: (Legend: > means push, -> means root vc)

a > b > c
c presents d
d > e > f

You should have:

navigationController1 -> a > b > c
c presents navigationController2
navigationController2 -> d > e > f

So embed each view controller you need present in a navigation controller. Then instantiate and present the proper navigation controller from your storyboard when you need to start a new flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM