简体   繁体   中英

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 ?

for example,

A(Base UIViewController ) open B( UIViewController ) and open 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 )

like this A->D

Would you give me some tips?

Well, when you will come from push, that means you will be coming from didReceiveRemoteNotification .

Write below line of code in 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

- (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 defaultCenter] postNotificationName:@"openMyView" object:nil userInfo:myInfoDic];

Then inside notification observer method, you can perform a segue to your UIViewController that needs to display

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

You have to create the segue with the mentioned name in storyboard too. Hope you are familiar that process already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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