0

My iOS application using Objective C. It received a remote notification, when a user click it, it will open a particular view.

This view has a back button in the navigation tab. In normal scenarios can back to root view. However, when I open this view from a notification, cannot back to any view nor leaving the application.

Here is my spruce code: 1. from AppDelegate.m to open a particular view:

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController *viewAnnouncement =[storyboard instantiateViewControllerWithIdentifier:@"Announcement"];

self.window.rootViewController = viewAnnouncement;
[self.window makeKeyAndVisible];
  1. Action function to back to previous view (in viewClass.m):

    - (IBAction)onBtnBackClicked:(id)sender {
        [self dismissViewControllerAnimated:YES completion:nil];
     }
    

I believe the problem is, when I open this view from a notification, no parent view to let it back to. Can anyone help me, when user open this view from a notification, whether it goes to a root view or back to the previous opening app

3 Answers3

0

EDIT : The previous answer didn't take in count the fact that you were in AppDelegate. Maybe this answer is better.


Turn your [self dismissViewControllerAnimated:YES completion:nil]

into :

UINavigationController *navigationController = [[UINavigationController alloc] init];
UIViewController *yourPreviousVC = [[UIViewController alloc] init];

    [navigationController pushViewController:yourPreviousVC animated:YES];

If I'm wrong or if I missunderstood the question, do not hesitate to correct my answer.

0

Try this

Announcement *annouce = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Announcement"];
HomeVC *home = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HomeVC"];
[((UINavigationController *)self.window.rootViewController) setViewControllers:@[home,annouce] animated:YES];

And If app already open and you want to go previous page

Announcement *annouce = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Announcement"];
UIViewController *visibleView = ((UINavigationController *)window.rootViewController).visibleViewController;
[((UINavigationController *)self.window.rootViewController) setViewControllers:@[visibleView,annouce] animated:YES];
vp2698
  • 1,783
  • 26
  • 28
0

I found the solution. My app has multiple views which I can go from a navigation list. When I want to back to main view, I just dismiss the current view.

Cuurently, a view is opened when I click a notification and I can't simply dismiss this view, Coz it considered as root view.

What I did to solve it, I connect my view (Announcement) by segue to main view, and set an identifier (for example: "leaveAnnouncementPage". Then, in your class just call this function in back button's fnction to back to the main view:

[self performSegueWithIdentifier:@"leaveAnnouncementPage" sender:self];

Here is also some useful links:

First

Second

Thank you guys for your help, and all the best