3

application:didFinishLaunchingWithOptions:我初始化一个UINavigationController. 稍后,我将其添加UINavigationController到窗口中:

[self.window addSubview:navigationController.view]

这一切都很好。现在我向我的应用程序添加了本地通知,当用户响应一个通知时,我想呈现一个UIViewController. 所以我想我可以覆盖application:didReceiveLocalNotification:并在那里使用我的navigationController

[navigationController pushViewController:someVC animated:YES];

但是,这不起作用。我做了一些调试,注意到虽然navigationController不是nilnavigationController.view没有超级视图,所以我认为它没有被显示。

所以,我的问题是:我应该把我的推到哪里才能UIViewController显示出来?

4

2 回答 2

5

在你的 AppDelegate.h 添加这个:

//Under where you have <UIKit/UIKit.h>
extern NSString *localReceived;

在您的 AppDelegate.m 中添加以下内容:

//All the way on top where you import your viewControllers
NSString *localReceived = @"localReceived";

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

        [[NSNotificationCenter defaultCenter] postNotificationName:localReceived object:self];

确保您的 viewController 是一个 navigationController

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

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

[window addSubview:nvcontrol.view];
[window makeKeyAndVisible];

现在 - 在您的 viewController.m 中将其添加到您的 -viewDidLoad 函数中

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localAction) name:localReceived object:nil];

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

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

于 2011-05-16T17:20:59.827 回答
2

所以这就是另一种采用不同方法的解决方案。它对我来说就像一个魅力。一探究竟:

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    CustomViewController *cvc = (CustomViewController *)[storyboard instantiateViewControllerWithIdentifier:@"CustomVC"];
    AnotherViewController *avc = (AnotherViewController *)[storyboard instantiateViewControllerWithIdentifier:@"AnotherVC"];
    avc.someValue = @"Passing a value"; //Optional
    UINavigationController *nav = (UINavigationController *) self.window.rootViewController;
    nav.viewControllers = [NSArray arrayWithObjects:cvc,avc, nil];
    [(UINavigationController *)self.window.rootViewController popToViewController:avc animated:TRUE];
    [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; 
    //Cancel Just for not showing it anymore on the notifications list...
}
于 2013-02-18T23:41:46.653 回答