[英]When user tap push notification, what is the best way to close current viewcontroller and open new viewcontroller
When user tap push notification, what is the best way to close current UIViewController
and open new UIViewController
? 当用户点击推送通知时,关闭当前UIViewController
并打开新UIViewController
的最佳方法是什么?
for example, 例如,
A(Base UIViewController
) open B( UIViewController
) and open C( UIViewController
) A(Base UIViewController
)打开B( UIViewController
)并打开C( UIViewController
)
and use tap home button, so app will enter background, 并使用主屏幕按钮,应用将进入后台,
after then when a user receive a push notification, and tap it, 之后,当用户收到推送通知时,点击该通知,
app will enter foreground. 应用程序将进入前台。
I want to close B, C UIViewController
(C controller may be opened or not), and open D( UIViewController
) 我想关闭B,C UIViewController
(可以打开或不打开C控制器),然后打开D( UIViewController
)
like this A->D 像这样A-> D
Would you give me some tips? 你能给我一些提示吗?
Well, when you will come from push, that means you will be coming from didReceiveRemoteNotification
. 好吧,当您来自推入时,这意味着您将来自didReceiveRemoteNotification
。
Write below line of code in 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];
I hope this is clear. 我希望这很清楚。
Note: As you are accessing navigation controller in app delegate, use below to access navigation controller. 注意:当您在应用程序委托中访问导航控制器时,请使用以下内容访问导航控制器。
(UINavigationController *)self.window.rootViewController
The method I've followed was, in AppDelegate.m file you can implement the 我遵循的方法是,在AppDelegate.m文件中,您可以实现
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Inside that you can have mechanism to identify the type of notification and according to that you can post your own notification using NSNotificationCenter 在其中,您可以使用机制来识别通知的类型,并根据该机制可以使用NSNotificationCenter发布自己的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"openMyView" object:nil userInfo:myInfoDic];
Then inside notification observer method, you can perform a segue to your UIViewController that needs to display 然后在通知观察器方法中,您可以对需要显示的UIViewController执行segue
[self performSegueWithIdentifier:@"toMySegue" sender:myDataDic];
You have to create the segue with the mentioned name in storyboard too. 您还必须在情节提要中使用提及的名称创建segue。 Hope you are familiar that process already. 希望您已经熟悉该过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.