【问题标题】:Why is my presentModalViewController not working from inside AppDelegate?为什么我的 presentModalViewController 不能在 AppDelegate 内部工作?
【发布时间】:2012-02-29 23:08:17
【问题描述】:

为什么要在 AppDelegate 中使用 presentModalViewController? - 处理 didReceiveLocalNotification,所以我可以在我的应用程序顶部启动一个单独的 modalView 来处理通知

我的视图架构是什么样的? - 使用故事板 - 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];

【问题讨论】:

  • self.window.rootViewController.tabBarController.selectedViewController.navigationController == nil ?
  • 谢谢。该死的,是的,刚刚测试过,实际上是零。难怪为什么它不起作用。但为什么它是零,我从这里去哪里?
  • [self.window.rootViewController presentModalViewController:navigationController animated:YES];
  • 确实有效。但是我在顶部有一个导航栏,但视图(屏幕的其余部分)是黑色的......
  • [self.window.rootViewController presentModalViewController:controller animated:YES];

标签: ios xcode storyboard


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    您是否尝试过以下方法?

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

    也就是说,即使这样可行,我也强烈建议您重新考虑您的设计选择,以避免不得不这样做。以这种方式遍历导航堆栈以访问内容会变得非常混乱,我强烈建议不要这样做。

    【讨论】:

    • 同样的事情,不起作用。视图永远不会被加载(未调用 ViewDidLoad)。
    • 我不知道其他开发人员如何显示模态视图来处理通知。换句话说,我不知道如何以另一种方式做到这一点。
    • 如何在适当的视图控制器上调用一个方法,如果可见,然后调用 presentModalViewController: ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2013-05-14
    • 2011-01-30
    相关资源
    最近更新 更多