简体   繁体   中英

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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM