Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is rootViewController & when do I use it?

while integrating a payment gateway in my iOS app, I used rootViewController property as below:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"navCtrlID"];

 [[UIApplication sharedApplication].keyWindow setRootViewController:controller];

 [self.navigationController presentViewController:controller animated:YES completion:nil];

It does the job, however now I have to press the back button multiple times to go back.

Why is it so?

like image 410
ios_Dev Avatar asked Oct 23 '25 17:10

ios_Dev


1 Answers

The RootViewController is the first ViewController on the Application Stack. You should only set it in your AppDelegate on the method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

In your case, since you instantiate it from Storyboard, the NavigationController with ID "navCtrlID" will be displayed first. If it only has one ViewController, when you press back it shouldn't pop the navigation stack.

If you have ViewControllers presented before adding this particular view you shouldn't show it like that. Instead, use for example:

[self presentViewController: controller animated:YES completion:nil];
like image 54
LopesFigueiredo Avatar answered Oct 26 '25 06:10

LopesFigueiredo



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!