管理多个UINavigationController堆栈的最佳实践

时间:2014-06-24 13:11:37

标签: ios objective-c uiviewcontroller uinavigationcontroller

我有一个应用程序,我有3个UINavigation堆栈,可以通过自定义菜单在彼此之间切换。当从堆栈A切换到堆栈B或C时,看起来新部分被推送到当前导航堆栈,因为堆栈B / C的RootViewController都具有回弹到前一堆栈的后退按钮。用户可以使用自定义菜单导航回堆栈A,也可以点击Stack B / C RootViewController上的后退按钮,将它们带回到堆栈A中的位置。

我遇到的问题是找出跟踪用户是否在堆栈A中的最佳方法。如果他们在堆栈A中的第4个向下钻取,切换到堆栈B,然后切换回在Stack A中,我需要准确地显示他们之前在Stack A的流程中的位置。

我应该使用多个UINavigationController,还是有一种方法可以在没有那么多麻烦的情况下实现这一点(即可能使用UIViewController遏制)?

2 个答案:

答案 0 :(得分:1)

您可以使用遏制以某种方式更改导航控制器,是的,但您当然不需要这样做。您可以在替换它之前获取UINavigationController中的整个堆栈,并在类似数组或字典的结构中跟踪您的3个堆栈。

typedef {
   Section1,
   Section2,
   Section3
} Section;

..

@property (nonatomic, assign) Section currentSection;
@property (nonatomic, strong) NSMutableArray currentStacks; //Initialize this was the base stacks for each section (i.e an NSArray with just 1 controller for each slot)
@property (nonatomic, strong) UINavigationController *navigationController;

..

- (void)setSection:(Section)section
{
    self.stacks[self.currentSection] = [self.navigationController.controllers copy];//Save stack for the current section
    [self.navigationController setViewController:self.stacks[section] animated:YES];
    self.currentSection = section;
}

答案 1 :(得分:0)

只要你没有任何性能问题,我的主张是将所有必需的视图控制器堆栈保存在单独的 UINavigationControl -s中。因此,您将拥有与菜单中的项目一样多的导航控制器对象。在这种情况下,你会遇到更少的问题,你的逻辑会更清晰。

这种方法不需要太多内存,因为最易消耗的东西是 UIView 层次结构而不是 UIViewController -s或 UINavigationController -s