iOS:在 NSUserDefaults 中使用布尔值

iOS: Use a boolean in NSUserDefaults(iOS:在 NSUserDefaults 中使用布尔值)
本文介绍了iOS:在 NSUserDefaults 中使用布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序的 rootViewController 加载时,我希望能够检查用户登录凭据是否已保存到 NSUserDefaults.

When the rootViewController of my application is loaded, I want to be able to check whether or not the users login credentials have been saved to NSUserDefaults.

基本上,当用户加载应用程序并且他/她没有保存她的登录凭据时,将推送 modalAlertView 并且用户将能够适当地保存他们的凭据.这会将那些 UITextField 字符串保存到相应的 NSUserDefault 对象中.但是,是否有可能在完成此保存后,我可以创建一个 NSUserDefault 对象,它是一个布尔值并将值更改为是?

Basically, when the user loads the application and he/she doesn't have her login credentials saved, a modalAlertView will be pushed and the user will be able to save their credentials appropriately. This saves those UITextField strings to a respective NSUserDefault object. However, is it possible, that when this saving is done, I can create an NSUserDefault object which is a boolean and change the value to a yes?

意味着布尔值已经设置为否,并且当用户保存其登录凭据时,它也会将布尔值更改为是?

Meaning that the boolean is already set to no, and when the user saves their login credentials, it also changes the boolean to a yes?

推荐答案

您可以使用以下方法设置布尔值:

You can set your boolean by using:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"logged_in"];
[[NSUserDefaults standardUserDefaults] synchronize];

并使用以下代码阅读:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
    [self displayLogin];
} else {
    [self displayMainScreen];
}

这篇关于iOS:在 NSUserDefaults 中使用布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)