How can I pop a view from a UINavigationController and replace it with another in one operation?

后端 未结 16 2180
谎友^
谎友^ 2020-11-27 10:01

I have an application where I need to remove one view from the stack of a UINavigationController and replace it with another. The situation is that the first view creates an

16条回答
  •  眼角桃花
    2020-11-27 10:18

    After much effort (and tweaking the code from Kevin), I finally figured out how to do this in the view controller that is being popped from the stack. The problem that I was having was that self.navigationController was returning nil after I removed the last object from the controllers array. I think it was due to this line in the documentation for UIViewController on the instance method navigationController "Only returns a navigation controller if the view controller is in its stack."

    I think that once the current view controller is removed from the stack, its navigationController method will return nil.

    Here is the adjusted code that works:

    UINavigationController *navController = self.navigationController;
    MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];
    
    NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
    [controllers removeLastObject];
    navController.viewControllers = controllers;
    [navController pushViewController:mevc animated: YES];
    

提交回复
热议问题