ios navigation Stack Manipulation

2019-01-26 02:05发布

i am having issues try to manipulate the navigationstack from an ios app. Or at least with the behaviour as a result of that manipulation.

My Situation:

i have 3 ViewControllers.

Controller a shows multiple levels, Controller b is the gameview Controller c is some sort of Score

Obviously i will select a level in controller a, which triggers a segue to controller b, once the level is finished ill segue to controller c. every segue as a push.

Now once im in controller c i dont want to be able to go back to b using the back button. I want to move back to controller a. In order for this to work, i removed the controller from the stack, so back wont move to controller b. This works fine.

The issue im facing is that the backbutton does show on controller a, which seems off since there shouldn't be any back. If i click the backbutton, the app doesnt crash, the button just disappears leaving the title.

i tried adding :

    NSArray* controllers = [self.navigationController viewControllers];

    if ([controllers count]<=1) {
        [self.navigationItem setHidesBackButton:YES animated:YES];
    } else {
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }
    [super viewDidAppear:animated];

as suggested in some relative stackoverflow article, without success. Besides this not working it seems off that ios creates those buttons from Storyboard without me actually adding them, but doesnt remove them when they arent necessary anymore. This leaves me with some options.

  • either i think that ios is smarter than it actually is
  • i am missing something essential to update the navigationbar
  • i went at this all wrong

besdies im using this code snipped to segue from Controller b to c.

[self performSegueWithIdentifier:@"feedbackSegue" sender:self];
[self removeFromParentViewController];

Any hints concerning missing operations or general bad practice is greatly appreciated.

Update

After further investigation, its not just the back button, its the whole navigationbar that is off. it behaves as if the removed controller was still there. The BackButton is there and another uiActionButton on the right end.

Does the navigationbar store its states onto a different stack, than the viewcontroller one? if this was the case, i could remove that state from this stack as well, to keep it consistent.

2条回答
不美不萌又怎样
2楼-- · 2019-01-26 02:29

I believe the "correct" way to do this is to remove the back button on controller c. Depending on how and when you are removing controller b, you may be corrupting the navigation controller stack. It's generally not a good practice to manipulate the view controller stack.

To remove the back button, you have the correct code:

self.navigationItem.hidesBackButton = YES;

However, note that you must call this before the view controller is presented -- i.e., in something like viewDidLoad.

When you want to pop back to A, use:

[self.navigationController popToRootViewControllerAnimated:YES];
查看更多
神经病院院长
3楼-- · 2019-01-26 02:39

You could try this in your view controller c. This will remove the previous view controller, in your case the b. You'd also have to keep b in your stack (remove the line [self removeFromParentViewController]'; )

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if(self.navigationController.viewControllers.count>2){
        NSArray *controllers = self.navigationController.viewControllers;
        NSMutableArray *newViewControllers = [NSMutableArray arrayWithArray:controllers];
        [newViewControllers removeObject:[controllers objectAtIndex:self.navigationController.viewControllers.count - 2]];
        self.navigationController.viewControllers = newViewControllers;
    }
}
查看更多
登录 后发表回答