How to use custom Navigationcontroller in AppDeleg

2019-04-08 12:02发布

I've got a problem concerning Navigationcontroller in AppDelegate. I'm using a storyboard, which looks like this:

Storyboard

As a result of using Push notifications, i've got the following function in my AppDelegate File:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//...
}

When the notification arrives I want to initialize the "Detail View" - Controller which needs an ID as a parameter. This ID is part of my payload so it is present in didReceiveRemoteNotification.

I'd like to to the follwing:

DetailView *detail = [storyboard instantiateViewControllerWithIdentifier:@"detail"];

detail.conversationID = theID; 

[self.navigationController pushViewController:detail animated:YES];

My question at this point is: how can I get the navigation controller? I've searched for a function like "getNavigationControllerByIdentifier" or something like this, but found nothing. I can't instantiate the Detail View Controller directly because the navigationbar is missing there.

I hope you understand what I mean - if you think my approach is completly wrong please correct me ;o)

Just another small information: It's not important for me that the back button in the Detail View Controller goes back to the Table View - it's enough when it links to the controller with the button "Load Table View".

Thank you for help!

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-08 12:12

You can use rootViewController on your window object.

UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.

// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;

// You can now use it to push your next view controller:
DetailViewController *detail = [navigationController.storyboard instantiateViewControllerWithIdentifier:@"detail"];
detail.conversationID = theID; 
[navigationController pushViewController:detailViewController animated:YES];
查看更多
Deceive 欺骗
3楼-- · 2019-04-08 12:18

UINavigationController is a UIViewController subclass and can also be assigned an identifier in the storyboard.

Use -instantiateViewControllerWithIdentifier: to create the UINavigationController and it's root view controller. You may need to instantiate all of the intermediate controllers in code and modify the navigation controller's viewControllers property to set up the appropriate navigation stack. This way when the app launches into the detail view, they can find their way back as if they had manually pushed all the way through via the interface.

查看更多
登录 后发表回答