如何很好地退出iPhone应用程序? [英] How to quit an iPhone app nicely?

查看:165
本文介绍了如何很好地退出iPhone应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或如何模拟主页按钮按下事件?

or "How to simulate a home button pressed event?"

我需要重新启动我的iPhone应用程序,我希望该程序退出,因此用户将只需要启动它。

I need to restart my iPhone app, and I want the program to quit, so the user will only have to start it.

如果我只是使用 exit(0)某些更改将无法保存,如果用户通过按主页按钮退出,就会如此。

If I simply use exit(0) some changes won't get saved, as they would if the user quits by pressing the home button.

语言更改需要重新启动。

The restart needed for language change.

相关代码:

- (void)onChangeLanguage: (id)sender {
    NSArray *lang = [NSArray arrayWithObjects:((Whatever *)sender).newLanguage, nil];
    [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLanguage = [languages objectAtIndex:0];

    NSLog(@"Current language: %@", currentLanguage);
    // ***
}   

如果用户重新开始使用房屋按钮,语言将改变。

If the user restarts using the home button, language will change.

如果 // *** 替换退出(0),语言不会改变。

If // *** is replaced by exit(0), the language won't change.

推荐答案

我认为打电话完全没问题退出,在执行此操作之前,只需调用 [[NSUserDefaults standardUserDefaults] synchronize] 。您可以在 synchronize 方法/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUserDefaults/synchronize\"rel =nofollow noreferrer> Apple文档

I think it’s perfectly fine to call exit, just call [[NSUserDefaults standardUserDefaults] synchronize] before you do that. You can read about the synchronize method in the Apple Documentation:


因为这个方法是自动定期调用
,所以只有当你不能等待
进行自动同步时才使用
这个方法(对于
例如,如果您的应用程序大约是
退出)或者如果您想要更新
用户默认值为磁盘上的内容,即使您没有进行任何更改,也需要

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

这篇关于如何很好地退出iPhone应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆