我有一个登录屏幕,提示用户登录。第一次成功登录后,我需要应用程序在用户再次打开应用程序时取消登录屏幕。我正在使用NSUserDefaults将成功登录设置为true。但是,始终向用户显示登录屏幕。请帮忙。感谢。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (NSUserDefaults.standardUserDefaults().objectForKey("onoroff") != nil) {
self.performSegueWithIdentifier("segue", sender: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func button(sender: AnyObject) {
if ((loginid.text == "Hussain") && (passwordText.text == "1234" ))
{
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
self.performSegueWithIdentifier("segue", sender: nil) }
else
{
failedtext.hidden = false }
}
答案 0 :(得分:0)
将objectForKey更改为boolForKey
if (NSUserDefaults.standardUserDefaults().boolForKey("onoroff")) {
self.performSegueWithIdentifier("segue", sender: nil)
}
也这样做
@IBAction func button(sender: AnyObject) {
if ((loginid.text == "Hussain") && (passwordText.text == "1234" ))
{
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
NSUserDefaults.standardUserDefaults().synchronize()
self.performSegueWithIdentifier("segue", sender: nil)
}
else
{
failedtext.hidden = false
}
}
答案 1 :(得分:0)
尝试添加
if (NSUserDefaults.standardUserDefaults().boolForKey("onoroff"))) {
self.performSegueWithIdentifier("segue", sender: nil)
}
在viewDidAppear方法而不是viewDidLoad