Objective C: How to present modal view controller from appdelegate?

大憨熊 提交于 2020-01-12 03:42:24

问题


I am in the appdelegate of my application. How can I add a modal view controller in the "didfinishlaunching" method?

I tried the following but did not work

SomeViewController *vc = [[SomeViewController alloc]init];
[self.tabController.navigationController presentModalViewController:vc animated:NO]; 

EDIT: I changed my implementation to the following

self.tabController.selectedViewController 
= [self.tabController.viewControllers objectAtIndex:0];
SomeViewController *vc = [[SomeViewController alloc]init];
[self.tabController.selectedViewController presentModalViewController:vc animated:NO];

I checked that the 'selected view controller' is not null... however I am still not able to get the output I needed. Is there anything I am missing?


回答1:


Assuming tabController and navigationController are not nil, the applicationDidFinishLaunching may be too soon to display the modal view controller.

  1. Make sure you put that code after you make the window key and visible. [self.window makeKeyAndVisible];
  2. If that does not work try listening for the UIWindowDidBecomeKeyNotification for that window
  3. You can try delaying presentation of that modal a few seconds using performSelector:withObject:afterDelay:


来源:https://stackoverflow.com/questions/7067443/objective-c-how-to-present-modal-view-controller-from-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!