在本地通知后呈现 UIViewController

发布于 2024-11-07 13:16:50 字数 714 浏览 0 评论 0原文

application:didFinishLaunchingWithOptions: 中,我初始化了 UINavigationController。后来,我将 UINavigationController 添加到窗口:

[self.window addSubview:navigationController.view]

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

[navigationController pushViewController:someVC animated:YES];

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

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

In application:didFinishLaunchingWithOptions: I initialize a UINavigationController. Later, I add the UINavigationController to the window:

[self.window addSubview:navigationController.view]

This all works fine. Now I added local notifications to my app and when the user responds to one, I would like to present a UIViewController. So I thought I could override application:didReceiveLocalNotification: and in there, use my navigationController:

[navigationController pushViewController:someVC animated:YES];

However, this does not work. I did some debugging and noticed that while navigationController is not nil, navigationController.view has no superview, so I assume it is not being displayed.

So, my question is: where should I push my UIViewController so that it gets displayed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-11-14 13:16:50

在您的 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 代码以推送到下一个查看控制器!

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

In your AppDelegate.h add this:

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

In your AppDelegate.m add this:

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

In your AppDelegate.m in the - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification;
method add this:

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

Make sure that your viewController is a navigationController

If it's not, do the following -- Add this piece of code to your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

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

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

Now -- in your viewController.m add this to your -viewDidLoad function

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

Make a -(void) localAction and in that method add your navigationController code to push to the next View Controller!

Hope that works for you. Works like a charm for me

阳光下的泡沫是彩色的 2024-11-14 13:16:50

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

- (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...
}

So here it is, another solution with a different approach. It worked like a charm for me. check it out:

- (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...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文