簡體   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