How to communicate with UI views from app delegate?
I am currently using the AppDelegate in my first iPhone app to handle application level events.
In my app I have an enabled and disabled state, so when the user selects disabled some of the buttons on the applicati开发者_开发问答on should be set to disabled.
My AppDelegate gets notified of the state. But what is the best approach to let my UIViews know that they should disable some of their buttons?
What is the standard approach to communicate events from the AppDelegate to the UI screens?
EDIT:
My description is slightly wrong, the user can disable the buttons in my app but conditions such as lack of Wi-Fi can as well, so I need to be dynamically able to change the UI state also.
Use NSNotification
class to notify to your views.
Communicate using NSNotification
Communicate using NSNotificationCenter
I would say AppDelegate is always not the best approach. In my apps, rather I prefer Singleton
class, they are quite easy to use.
eg Singleton approach:
AppConfig* config = [AppConfig sharedInstance]; // AppConfig is my singleton instance
[[self usernameTextField] setText:[config studentName]];
[toggle setOn:[config alwaysShow]]; // toggle is UISwitch
hope this helps.
精彩评论