2012-02-29 40 views
3

爲什麼我想在AppDelegate中使用presentModalViewController? - 正在處理didReceiveLocalNotification,所以我可以在我的應用程序頂部啓動一個單獨的modalView來處理通知爲什麼我的presentModalViewController無法在AppDelegate中工作?

我的視圖體系結構是什麼樣子的? - 使用故事板 - MainStoryBoard: - > TabBarController-> NavigationController

發生了什麼? - 沒什麼,這就是問題所在:--D - 當我按下UILocalNotification中的動作按鈕時,應用程序打開,但只顯示tabbarcontroller的最後一個打開的視圖。

正如你可以看到下面我最後的力氣將存在modalViewController對當前視圖的頂部,就像這樣: [self.window.rootViewController.tabBarController.selectedViewController.navigationController presentModalViewController:navigationController animated:YES];

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

UIApplicationState state = [application applicationState]; 
if (state == UIApplicationStateInactive) { 
    // Application was in the background when notification was delivered. 
    NSLog(@"Received notification while in the background"); 
} 
else { 
    NSLog(@"Received notification while running."); 
} 

MedicationReminderViewController *controller = [[UIStoryboard storyboardWithName:@"ModalStoryBoard" bundle:nil] instantiateViewControllerWithIdentifier:@"MedicationReminderVC"]; 

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller]; 
[self.window.rootViewController.tabBarController.selectedViewController.navigationController presentModalViewController:navigationController animated:YES]; 

}

更新

看來,這是零: self.window.rootViewController.tabBarController.selectedViewController.navigationController

解決方案

[self.window.rootViewController presentModalViewController:navigationController animated:YES];

+0

self.window.rootViewController.tabBarController.selectedViewController.navigationController ==零? – NeverBe 2012-02-29 14:34:35

+0

謝謝。該死的,剛剛測試過,實際上是零。難怪爲什麼它不起作用。但爲什麼它是零,我從哪裏去? – Pieter 2012-02-29 14:42:05

+0

[self.window.rootViewController presentModalViewController:navigationController animated:YES]; – NeverBe 2012-02-29 14:48:04

回答

6

試試這個:

[self.window.rootViewController presentModalViewController:controller 
                animated:YES]; 
+0

謝謝你的作品 – 2013-11-21 16:24:11

0

您是否嘗試過以下方法?

[self.window.rootViewController.tabBarController.selectedViewController presentModalViewController:navigationController animated:YES]; 

即使這樣做,我真的會敦促你重新考慮你的設計選擇,以避免這樣做。以這種方式遍歷導航堆棧以訪問東西會變得非常混亂,我強烈建議不要這樣做。

+0

同樣的事情,不起作用。視圖永遠不會被加載(ViewDidLoad未被調用)。 – Pieter 2012-02-29 14:36:45

+0

我不知道其他開發者如何顯示處理通知的modalviews。換句話說,我不知道如何以另一種方式做到這一點。 – Pieter 2012-02-29 14:37:52

+0

如何在適當的視圖控制器上調用方法,如果可見,則調用presentModalViewController:? – 2012-02-29 14:54:16

相關問題