Yoav Schwartz
Yoav Schwartz

Reputation: 2027

influencing a view from the AppDelegate

i know it is a beginners question but i just couldn't find a solution. i have an app with a storyboard. that is a tabbed view app, and each tab leads to a nav view controller and than to a root view controller. in the nav view controller i have a Login button. pressing it sends a message to the app delegate to handle opening a session with Facebook. i want after the session with facebook opened, to set the title of the button to logout because thats what its going to do if i press it again. this was all visually programmed and not hardcoded so much of the code is missing. now i know that theres only one instance of the app delegate so its pretty easy to access it from the view, but how do i get from the app delegate to the active session of the view? i tried putting it in viewDidLoad method but apparently the callback from the Facebook API doesn't reload the view(as well it shouldn't, to not work the system again for nothing)

any ideas how to get the app delegate to send a messege to the view's activee session?

Upvotes: 0

Views: 116

Answers (1)

rickerbh
rickerbh

Reputation: 9913

Here are a couple of ways you could do this:

  1. You could use get your app delegate to post a notification when the user is logged in/out (the Facebook library might already post notifications?), and your view controllers could register for this notification so that they can change their state when the Facebook session changes state. You'd also need to do something on your viewDidLoad method so that if the user has a session from a previous login, the controller gets itself into the correct initial state. Check out https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html#//apple_ref/doc/uid/10000043i for more information on notification programming.
  2. You could implement code in viewWillAppear:animated on your view controller so that when the view is displayed after the user has logged in, the controller queries the app delegate to determine your session state. I suspect this is what you're trying to do by putting the code in viewDidLoad, but the view is already loaded so this code won't be called after a login. viewWillAppear:animated gets called just before the view appears.

Upvotes: 1

Related Questions