繁体   English   中英

当用户点击推送通知时,关闭当前ViewController并打开新ViewController的最佳方法是什么

[英]When user tap push notification, what is the best way to close current viewcontroller and open new viewcontroller

当用户点击推送通知时,关闭当前UIViewController并打开新UIViewController的最佳方法是什么?

例如,

A(Base UIViewController )打开B( UIViewController )并打开C( UIViewController

并使用主屏幕按钮,应用将进入后台,

之后,当用户收到推送通知时,点击该通知,

应用程序将进入前台。

我想关闭B,C UIViewController (可以打开或不打开C控制器),然后打开D( UIViewController

像这样A-> D

你能给我一些提示吗?

好吧,当您来自推入时,这意味着您将来自didReceiveRemoteNotification

didReceiveRemoteNotification编写以下代码行

NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];

[navigationArray removeAllObjects];    // This is just for remove all view controller from navigation stack.
// [navigationArray removeObjectAtIndex: 2];  // You can pass your index here
self.navigationController.viewControllers = navigationArray;
[navigationArray release];

我希望这很清楚。

注意:当您在应用程序委托中访问导航控制器时,请使用以下内容访问导航控制器。

(UINavigationController *)self.window.rootViewController

我遵循的方法是,在AppDelegate.m文件中,您可以实现

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

在其中,您可以使用机制来识别通知的类型,并根据该机制可以使用NSNotificationCenter发布自己的通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"openMyView" object:nil userInfo:myInfoDic];

然后在通知观察器方法中,您可以对需要显示的UIViewController执行segue

[self performSegueWithIdentifier:@"toMySegue" sender:myDataDic];

您还必须在情节提要中使用提及的名称创建segue。 希望您已经熟悉该过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM