只要我不关闭iPhone,applicationWillTerminate就可以正常工作 [英] applicationWillTerminate works as long as I don't switch off the iPhone
问题描述
保存我使用的应用程序的一些变量:
to save some variables of my apps I use:
- (void)applicationWillTerminate:(UIApplication *)application {
-(void)applicationWillTerminate:(UIApplication *)application {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat: self.viewController.sLabel.contentOffset.y forKey:@"floatKey"];
[prefs setObject:self.viewController.newText forKey:@"stringVal"];
[prefs synchronize];
}
并检索它们,通过按钮,我执行以下操作:
and to retrieve them, via a button, I do the following:
- (IBAction)riprendi:(id)发件人{
-(IBAction) riprendi:(id) sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float myFloat = [prefs floatForKey:@"floatKey"];
//这里有一些行动
}
一切都在模拟器上运行。但是,在真正的iPhone上使用它,变量的保存和检索只有按下主页按钮,退出应用程序并再次打开才有效,但如果关闭/打开iPhone则不行。在这种情况下,一旦你重新打开应用程序,变量就会丢失......
Everything is working on the simulator. However, using it on a real iPhone, the variables' saving and retrieving works just if you press the Home button, exiting the app and opening again but NOT if you switch off/on the iPhone. In this case, the variables get simply lost once you re-open the app...
我错过了什么?这实际上让我抓狂:(
What am I missing?? This is actually driving me crazy :(
非常感谢你;)
Fabio
Thank you so much ;) Fabio
推荐答案
如果你的意思是通过说打开/关闭按下手机顶部的锁定按钮,那么它将无法工作,因为锁定手机不会导致应用程序退出。
您的 applicationWillTerminate:
方法仅在您退出应用程序主屏幕或某个其他应用程序时调用。当用户按下睡眠按钮时, applicationWillResignActive:
将发送给您的应用程序代表。
If you mean hitting the lock button on the top of the phone by saying "switching on/off", then it won't work, because locking the phone does not cause an application to quit.
Your applicationWillTerminate:
method is only called when you exit you're application to the home screen or to some other application. When the user presses the Sleep button, applicationWillResignActive:
will be send to your application-delegate.
Apple的iPhone OS编程指南有一节处理中断。
这篇关于只要我不关闭iPhone,applicationWillTerminate就可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!