即使我在NSUSERdefault中设置了值,我也会变成空白用户使用锁定模式但是相同的代码不适用于App的超时(5分钟)用户是vcLogin.usernameTxt?.text = uname即将出现空白甚至NSUserDefaults.standardUserDefaults()。objectForKey(“Username”)as!字符串具有值
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vcLogin = storyboard.instantiateViewControllerWithIdentifier("Login") as! LoginViewController
var uname:String!
if (NSUserDefaults.standardUserDefaults().objectForKey("Username") != nil) {
print("username =\(NSUserDefaults.standardUserDefaults().objectForKey("Username"))")
uname = NSUserDefaults.standardUserDefaults().objectForKey("Username") as! String
NSUserDefaults.standardUserDefaults().synchronize()
}else{
uname = ""
}
let navVC = self.window?.rootViewController as? UINavigationController
var arr=[UIViewController]()
arr = (navVC?.viewControllers)!
if ((arr.last?.isKindOfClass(LoginViewController)) != nil) {
arr.append(vcLogin)
navVC?.viewControllers = arr
print("name=\(uname)")
//if uname != nil {
vcLogin.usernameTxt?.text = uname
//}
}
}
}
答案 0 :(得分:0)
let theUsername = NSUserDefaults.standardUserDefaults().objectForKey("Username")!
print("username =\(theUsername)")
顺便问一下,为什么要使用Swift 2?