Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the UINavigationController viewControllers stack?

I have 3 viewControllers, From the first i go to the second, and from the second to the third.

I want to go from the third view controller back to the first.

One solution I found is to create a custom button and replace the back button, this way I perform my own selector and go back to the firstVC. But the button I created doesnt look like the back button.

So one solution I thought about is:

Once I enter the third view controller, alter the viewControllers arrays in the navigationController removing the second view controller. This way it would go back to the first without changing the back button.

Is this possible? can I change the UINavigationController stack at will?

Thanks in advance

like image 563
pedros Avatar asked Jul 04 '26 10:07

pedros


2 Answers

You can, there is a special method for that - [UINavigationController setViewControllers:animated:].

like image 99
Sulthan Avatar answered Jul 07 '26 01:07

Sulthan


You can try to remove the second view controller from the stack and check it. Shouldn't be a problem.

NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[array removeObjectAtIndex:1];

self.navigationController.viewControllers = array;

Or just use

[self.navigationController setViewControllers:array animated:NO];

But this is not a recommended approach as the users might get confused. So be careful when you are doing it this way.

like image 36
iDev Avatar answered Jul 07 '26 01:07

iDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!