xcode 为什么我的presentModalViewController 不能从AppDelegate 内部工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9500537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:29:01  来源:igfitidea点击:

Why is my presentModalViewController not working from inside AppDelegate?

iosxcodestoryboard

提问by Pieter

Why do I want to use presentModalViewController in AppDelegate? - Processing didReceiveLocalNotification, so I can launch a seperate modalView on top of my app to process the notification

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

What does my view architecture look like? - Using storyboards - MainStoryBoard: ->TabBarController->NavigationController

我的视图架构是什么样的?- 使用故事板 - MainStoryBoard: ->TabBarController->NavigationController

What's happening? - Nothing, that's the problem :-D - When I press the action button from the UILocalNotification, the app opens, but just shows the last open view from the tabbarcontroller.

发生了什么?- 没什么,这就是问题 :-D - 当我从 UILocalNotification 按下操作按钮时,应用程序打开,但只显示 tabbarcontroller 的最后一个打开视图。

As you can see below my last effort was to present the modalViewController on top of that current view, like so: [self.window.rootViewController.tabBarController.selectedViewController.navigationController presentModalViewController:navigationController animated:YES];

正如你在下面看到的,我最后的努力是在当前视图的顶部呈现 modalViewController ,如下所示: [self.window.rootViewController.tabBarController.selectedViewController.navigationController presentModalViewController:navigationController animated:YES];

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

- (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];
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];

}

}

Update

更新

Seems that this is nil: self.window.rootViewController.tabBarController.selectedViewController.navigationController

似乎这是零: self.window.rootViewController.tabBarController.selectedViewController.navigationController

Solution

解决方案

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

[self.window.rootViewController presentModalViewController:navigationController 动画:YES];

回答by NeverBe

Try this :

尝试这个 :

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

回答by Will Pragnell

Have you tried the following?

您是否尝试过以下方法?

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

That said, even if this works, I would really urge you to reconsider your design choices to avoid having to do this. Traversing the navigation stack in this way to access stuff can get very messy and I'd strongly advise against it.

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