开发者

NavigationController overflow

开发者 https://www.devze.com 2023-03-18 16:32 出处:网络
I\'ve designed and build game based on UINavigationController. I have several ViewControllers like on the picture. You select NewGame from RootVC and you are ready to play. After finishing, you are as

I've designed and build game based on UINavigationController. I have several ViewControllers like on the picture. You select NewGame from RootVC and you are ready to play. After finishing, you are asking to go to the next board in NextBoardVC. After 10 boards you are going to next level by NextLevelVC. There is 5 Levels. Each level contains 10 boards. The problem is that I'm pushing each ViewController using pushViewController method. After 5 levels I have 52 VCs on the stack and sometimes application crashes.

GameVC contains many png's, and some sounds so it's quite heavy. I don't have any leaks (tested by Instruments)

Sorry, I dont have reputation so I cant upload images by stackoverflow.

This is an image:

NavigationController overflow

Textual representation of the view hierarchy:

RootVC 
--OptionsVC
--HowToPlayVC
--NewGameVC
  --GameVC
    --NextBoardVC
      --NextLevelVC
        --GameVC
          --NextBoardVC
              --NextLevelVC
                -开发者_如何学Go-GameVC
                  --NextBoardVC
                    --NextLevelVC
                      --GameVC
                        --... and so on many times

Is there better way to do the navigation, and eliminate crashes?


Why don't you just pop the controllers back off instead of just pushing more and more controllers on top, especially at those points where you're returning in your "loop"?


Don't forget that you can directly set view controllers on a navigation stack, using setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated.

So rather than pushing new levels onto the navigation stack you could replace one level with the next one by creating an array containing all your view controller up to that point plus your new level, and using the setViewControllers method above.


You are managing your view controller hierarchy incorrectly. A navigation controller should never have a loop as your diagram suggest yours does.

Also, a navigation controller should managed a visual hierarchy first and logical one second. E.g. The contact app, where a group list view pushes a list of contacts which pushes a contact detail which pushes one of several attribute detail edit views. Everytime you leave one view you pop that view and go back "up" the hierarchy to the previous view. You would never configure it go from one contact detail view to the next contact view just by pushing another contact detail view on top of the last contract detail view.

Likewise you need to pop the existing BoardVC or LevelVC from the stack before pushing the next.

(Key: here --> means push and <-- means pop)

To setup a the first level of a new game you would:

RootVC-->NewGameVC-->GameVC-->BoardVC-->LevelOneVC

... and then to add the next level you would first:

RootVC-->NewGameVC-->GameVC-->BoardVC<--LevelOneVC

... to get:

RootVC-->NewGameVC-->GameVC-->BoardVC

... and then:

RootVC-->NewGameVC-->GameVC-->BoardVC-->LevelTwoVC

... repeat for all other levels. You can do the same for each BoardVC as needed.

The trick here is to not use the slide transition which is the default navigation controller transition. That transition tells the user to expect this logical layout:

RootVC-->NewGameVC-->GameVC-->BoardVC-->LevelOneVC-->LevelTwoVC

... which is what is causing your problem. Instead, use another transition like fade or hide the transition all together.

By popping any views that are not necessary for the user's navigation, you ensure that you view controller stack is never more than 5 view controllers deep at anyone time and that therefore only 5 view controllers are needed in memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消