rootViewController在后台运行(nil?)

时间:2016-03-07 05:20:03

标签: ios objective-c iphone cocoa-touch uiwindow

我正在尝试将myViewController myVC的对象设置为AppDelegate上的窗口的rootViewController,如下所示:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    self.myVC=(myViewController*)self.window.rootViewController;
}

当应用程序运行时,这一切都正常工作,但在后台它根本不起作用。

有人知道我怎样才能让它在后台运作?

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个

修改

<强> AppDelegate.m

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

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"STORYBOARD_NAME" bundle:nil];
ViewController *ViewController = [storyboard instantiateViewControllerWithIdentifier:@"YOUR_IDENTIFIER"];[self.window makeKeyAndVisible];     
[self.window.rootViewController presentViewController:ViewController animated:YES completion:NULL];
return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground");
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:ADD_YOURCONTROLLER];

[self presentViewController:navController animated:YES completion:nil];
}

希望这有帮助。