![](/img/trans.png)
[英]How do I use a RootViewController when making an app without a ViewController?
[英]What is rootViewController & when do I use it?
在將付款網關集成到我的iOS應用中時,我使用了rootViewController
屬性,如下所示:
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];
它可以完成工作,但是現在我必須多次按返回按鈕才能返回。
為什么會這樣呢?
RootViewController是應用程序堆棧上的第一個ViewController。 您應該只在AppDelegate上的方法中進行設置:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
在您的情況下,由於您是從Storyboard實例化的,因此將首先顯示ID為“ navCtrlID”的NavigationController。 如果它只有一個ViewController,則當您按回它時,不應彈出導航堆棧。
如果在添加此特定視圖之前已顯示ViewControllers,則不應那樣顯示。 而是使用例如:
[self presentViewController: controller animated:YES completion:nil];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.