我在使用Xcode4的iPhone iOS 4上做一个项目。
我有一个带UIButton的视图。在viewDidLoad中设置UIButton的标题。因此,当应用程序启动时,Button会有一个标题。
然而,我的应用程序有一个设置包,按钮标题也可以在设置应用程序中更改。所以我打开了主页按钮,我的应用程序退出了,我转到设置应用程序,并设置了一个新的按钮标题。
当我退出设置并重新启动我的应用程序时,按钮标题不会刷新,它是旧的按钮标题。一切都和我离开时一样。
只有当我关闭iPhone,然后打开并重新启动我的应用程序(即在完全重启时)时,按钮才会显示在新标题处。
如何确保应用程序(未完全)重启时按钮标题发生变化?
谢谢。
发布于 2011-08-14 06:09:28
您想要更新applicationDidEnterForeground中的设置。它会在恢复时被调用。
编辑:有一个打字错误,应该是
- (void)applicationWillEnterForeground:(UIApplication *)application请参阅文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
发布于 2011-08-14 05:48:15
我错误地认为,在从设置应用程序切换回来后,当应用程序再次激活时,会向ViewController发送viewWillAppear消息。然而,当应用程序激活时,会发布一个通知,所以您应该能够通过注册ViewController来接收UIApplicationDidBecomeActiveNotification通知来完成您想要的事情:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshButtonTitle:) name:UIApplicationDidBecomeActiveNotification object:nil];然后在您的ViewController中创建适当的方法,以便在触发通知时调用:
- (void)refreshButtonTitle:(NSNotification *)notification {
// update the title of your button here
}发布于 2011-08-16 00:31:38
您可以注册以接收NSUserDefaultsDidChangeNotification%s,然后执行相应操作以更新您的UI。
https://stackoverflow.com/questions/7053422
复制相似问题