Stephen
Stephen

Reputation: 739

How do I access the navigation controller from app delegate?

I'm making an app with push notifications and I want the app to show a modal view controller or push a view onto the current navigation controller when a notification is received. I have the didReceiveRemoteNotification in my app delegate that gets the notification but now I can't push the view controller because I don't have access to the navigation controller (or anything I can use). I've seen other posts where they create the navigation controller and do everything in the app delegate but I've used the interface builder so it's not obvious to me how I can get access to it. What is the best way to push a new view controller or show a modal?

Thanks for any tips/suggestions!

Upvotes: 1

Views: 1418

Answers (3)

sucitivel
sucitivel

Reputation: 510

Easiest way would probably be to create a property of your app delegate of type uinavigationcontroller, and then just set it from one of the screens belonging to the navigation controller of your app. If you just want to have access to it, this works for me, and is really easy. There are obvious caveats, but assuming your application can wait to use the navi controller from the app delegate until after the first screen has launched... then you're fine i think.

From your Home View:

IQAppDelegate *appDelegate = (IQAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setNaviController:[self navigationController]];

Upvotes: 2

Bryan
Bryan

Reputation: 12190

You could re-post the information from the app delegate using NSNotificationCenter and have your view logic call addObserver so it receives this notification.

This way, you can have different parts of your UI respond in different ways without centralising that knowledge in the app delegate.

Upvotes: 1

Stavash
Stavash

Reputation: 14304

Instead of trying to access the Navigation Controller via the AppDelegate, I would recommend adding a Singleton class that handles screens throughout your application. This screen manager would be in charge of adding / removing / switching between views while making sure that all your view controllers are released properly when done with. This way, you could access the current screen you are on, and present a modal view controller like you say you want (Make sure all your view controllers implement a specific protocol that declares a method to do this, for the sake of clean & correct coding). Good luck!

Upvotes: 0

Related Questions