开发者

How apply Setting changes at restart?

开发者 https://www.devze.com 2023-03-27 18:28 出处:网络
I work on a project on iPhone iOS 4 with Xcode 4. I have a view with a UIButton. The title of the UIButton is set in viewDidLoad. So when app starts, the Button has a title.

I work on a project on iPhone iOS 4 with Xcode 4.

I have a view with a UIButton. The title of the UIButton is set in viewDidLoad. So when app starts, the Button has a title.

However, my app has a Settings bundle and the button title can be also changed in Settings app. So I clic on the home button, my app quits, I go to the Settings app, and set a new button title.

When I quit Settings and restart my app, the button title is not refreshed, it is the old button title. Everything is as I had left.

Only if I turn off the iPhone, then turn on and relaunch my app (i.e. at full restart) the button has at the new title.

How to make sure that the button title changes when the app开发者_开发知识库 is (not full) restarted?

Thank you.


You want to update the settings in applicationDidEnterForeground. That gets called when it resumes.

Edit: Had a typo, it should be

- (void)applicationWillEnterForeground:(UIApplication *)application

see docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html


I was mistaken in thinking that the viewWillAppear message would be sent to the ViewController when the app became active again after switching back from the Settings app. There is a notification posted when the app becomes active, however, so you should be able to accomplish what you want by registering your ViewController to receive UIApplicationDidBecomeActiveNotification notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshButtonTitle:) name:UIApplicationDidBecomeActiveNotification object:nil];

and then create the appropriate method in your ViewController to be called when the notification is fired:

- (void)refreshButtonTitle:(NSNotification *)notification {
// update the title of your button here
}


You can register to receive NSUserDefaultsDidChangeNotifications and then act accordingly to update your UI.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号