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

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

    I had to do a similar thing recently and based my solution on Michaels answer. In my case I had to remove two View Controllers from the Navigation Stack and then add a new View Controller on. Calling

    [controllers removeLastObject];
    twice, worked fine in my case.

    UINavigationController *navController = self.navigationController;
    
    // retain ourselves so that the controller will still exist once it's popped off
    [[self retain] autorelease];
    
    searchViewController = [[SearchViewController alloc] init];    
    NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
    
    [controllers removeLastObject];
    // In my case I want to go up two, then push one..
    [controllers removeLastObject];
    navController.viewControllers = controllers;
    
    NSLog(@"controllers: %@",controllers);
    controllers = nil;
    
    [navController pushViewController:searchViewController animated: NO];
    

提交回复
热议问题