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

后端 未结 16 2095
谎友^
谎友^ 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:14

    From experience, you're going to have to fiddle with the UINavigationController's viewControllers property directly. Something like this should work:

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

    Note: I changed the retain/release to a retain/autorelease as that's just generally more robust - if an exception occurs between the retain/release you'll leak self, but autorelease takes care of that.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2020-11-27 10:23

    I use this solution to keep the animation.

    [self.navigationController pushViewController:controller animated:YES];
    NSMutableArray *newControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [newControllers removeObject:newControllers[newControllers.count - 2]];
    [self.navigationController setViewControllers:newControllers];
    
    0 讨论(0)
  • 2020-11-27 10:26

    Thanks, this was exactly what I needed. I also put this in an animation to get the page curl:

            MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];
    
        UINavigationController *navController = self.navigationController;      
        [[self retain] autorelease];
    
        [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration: 0.7];
        [UIView setAnimationTransition:<#UIViewAnimationTransitionCurlDown#> forView:navController.view cache:NO];
    
        [navController popViewControllerAnimated:NO];
        [navController pushViewController:mevc animated:NO];
    
        [UIView commitAnimations];
    

    0.6 duration is fast, good for 3GS and newer, 0.8 is still a bit too fast for 3G..

    Johan

    0 讨论(0)
  • 2020-11-27 10:26
    NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
        for(int i=0;i<controllers.count;i++){
           [controllers removeLastObject];
        }
     self.navigationController.viewControllers = controllers;
    
    0 讨论(0)
  • 2020-11-27 10:26

    For monotouch / xamarin IOS:

    inside UISplitViewController class;

    UINavigationController mainNav = this._navController; 
    //List<UIViewController> controllers = mainNav.ViewControllers.ToList();
    mainNav.ViewControllers = new UIViewController[] { }; 
    mainNav.PushViewController(detail, true);//to have the animation
    
    0 讨论(0)
提交回复
热议问题