2014-03-28 27 views
0

我的應用程序具有嵌入式導航控制器。我想啓動不是初始視圖或rootViewController的特定viewController。當我想從推送通知中啓動時,如何處理rootViews

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
{ 
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

if (dictionary != nil) 
{ 
    UIViewController *rootController = (UIViewController *)self.window.rootViewController; 
UIViewController *notificationController = [rootController.storyboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"]; 
[rootController presentViewController:notificationController animated:NO completion:^{ 

}]; 
} 
} 

我知道我必須在這裏輸入代碼,但我不知道我能如何編寫代碼。有人可以幫忙嗎?我得到錯誤代碼:

Warning: Attempt to present <NotificationsViewController: 0x176883a0> on <SWRevealViewController: 0x17683750> whose view is not in the window hierarchy! 

SWRevealViewController是我的庫,用於我的側欄視圖。我的猜測是我的「根」不是我認爲的那樣。

如果我想將viewControllerX設置爲我的根視圖,我該怎麼做到這一點?

+0

一件事情需要問。你的應用程序在沒有選項的情況下運行時如何工作從我所看到的情況來看,當launchOptions爲零時,沒有代碼可用。 接下來的事情是你需要在調用self.window.rootViewController之前先創建一個UIWindow實例。 – nsuinteger

回答

0

你應該首先初始化你的rootViewController。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
YourViewControllerX * viewControllerX = [[YourViewControllerX alloc] init]; 
self.window.rootViewController = viewControllerX; 
[self.window makeKeyAndVisible]; 
0

嘗試

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions{ 
    [self.window addSubview:self.window.rootViewController.view]; 
    [self.window makeKeyAndVisible]; 
    NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    if (dictionary != nil){ 
     ... 
    } 
}