在本地通知后呈现UIViewController

fab*_*789 2 iphone uiviewcontroller uinavigationcontroller ipad uilocalnotification

application:didFinishLaunchingWithOptions:我初始化一个UINavigationController.后来,我添加UINavigationController到窗口:

[self.window addSubview:navigationController.view]
Run Code Online (Sandbox Code Playgroud)

一切正常.现在我向我的应用添加了本地通知,当用户响应一个时,我想提出一个UIViewController.所以我想我可以覆盖application:didReceiveLocalNotification:并在那里使用我的navigationController:

[navigationController pushViewController:someVC animated:YES];
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用.我做了一些调试,并注意到虽然navigationController不是nil,navigationController.view但没有superview,所以我认为它没有被显示.

所以,我的问题是:我应该在哪里推动UIViewController它以便显示它?

sha*_*irv 5

在你的AppDelegate.h中添加:

//Under where you have <UIKit/UIKit.h>
extern NSString *localReceived;
Run Code Online (Sandbox Code Playgroud)

在你的AppDelegate.m中添加:

//All the way on top where you import your viewControllers
NSString *localReceived = @"localReceived";
Run Code Online (Sandbox Code Playgroud)

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification; 方法的AppDelegate.m中添加以下内容:

        [[NSNotificationCenter defaultCenter] postNotificationName:localReceived object:self];
Run Code Online (Sandbox Code Playgroud)

确保您的viewController是一个navigationController

如果不是,请执行以下操作 - 将以下代码添加到您的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

    UINavigationController *nvcontrol = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:nvcontrol.view];
[window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

现在 - 在你的viewController.m中将它添加到你的-viewDidLoad函数中

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localAction) name:localReceived object:nil];
Run Code Online (Sandbox Code Playgroud)

创建一个 - (void)localAction并在该方法中添加您的navigationController代码以推送到下一个View Controller!

希望对你有用.对我来说就像一个魅力