简体   繁体   English

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

[英]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.

相关问题 Swift 3 - 关闭当前的 ViewController 并打开新的 ViewController - Swift 3 - Close current ViewController and Open new ViewController 当用户点击 iOS Swift 中的通知时,如何加载 viewController? - How to load viewController when user tap on notification in iOS Swift? 从推送通知 IOS 打开应用程序时打开特定的 ViewController - Open specific ViewController when open app from push notification IOS 推送通知 - 在通知点击时使用 SceneDelegate 推送一个 ViewController - Push Notifications - Push a ViewController upon Notification Tap with SceneDelegate iOS点击推送通知打开具体的viewcontroller - iOS click on push notification open concrete viewcontroller 为什么当我点击推送通知并快速移动ViewController时状态栏消失 - Why status bar disappear when I tap push notification and move ViewController in swift 从当前的视图控制器中打开一个新的ViewController - Open a new ViewController from a current view controller 在图像上点击,打开另一个ViewController - On images tap, open another ViewController 轻按单元格时将值推送到新的ViewController - Push the values to new ViewController when a cell is tapped 恢复ViewController状态的最佳方法是什么 - What is the best way to restore the state of ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM