how to present a navigation controller embedded in

2019-08-11 11:39发布

问题:

I'm working on a project with a tab bar controller and push notifications, when my app receives a push notifications depending of the alert body it will open a determinate view controller that is embedded in tab bar.

I already can present the view controller but i lost the tab bar.

For presenting the view controller this is what i do:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            vc5 *ivc = [storyboard instantiateViewControllerWithIdentifier:@"vc5"];
            UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:ivc];
            self.window.rootViewController =nil;
            self.window.rootViewController = navigationController;
            [self.window makeKeyAndVisible];

I tried this code to show the tab bar but i didn't work:

UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:@"tabBarController" bundle:nil];
    tabBarController.selectedViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"];
    tabBarController.selectedViewController = nil;
    tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:0];
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    [tabBar setSelectedIndex:0];

My Storyboard consists of an Initial Login ViewController that is inside a navigation controller, after the user logins is prompt to pick a city and then i present a tab bar controller that consists o 4 tabs, the view that i need to present via push notification is in the first tab and is the fifth view inside that navigation controller.

I look out other questions here in SO but nothing works, this is the first time i develop in iOS, any help would be highly appreciated. thank you


EDIT: I also try to make a segue from the AppDelegate when i receive the notification but i get this error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<TabBarController: 0x16dec150>) has no segue with identifier 'pushNotification'

This is my code for the push:

TabBarController *tabBar = [[TabBarController alloc] initWithNibName:@"tabBarController" bundle:nil];
    [tabBar performSegueWithIdentifier:@"pushNotification" sender:self];

回答1:

Perform the segue from the initial login view controller, not from the app delegate. In order to do a push segue using storyboards you have to name your segue and view in storyboards. You can name these items by selecting them and using the property panel on the right in xCode.



回答2:

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

//Create view controller for Main here
UIViewController * MainNC = [storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];

[self presentViewController:MainNC animated:NO completion:nil];


回答3:

I resolved this by presenting the view controller when the notification arrives and pass the necessary data to the view controller