如果这是一个noobie问题再次道歉,但仍然相对较新,所以请耐心等待,
我试图在用户离开页面或关闭应用程序后更改UIViewController中的图像,这个想法是图像被按下密码输入并且图像被更改,(我已经完成了dzk的帮助)和图像应该改变。
但是当我离开应用页面然后再回来时,它已经重置为原始图像,非常令人沮丧!
下面是代码,它将在UIAlertController验证后更改图像。
任何帮助都会感激不尽。
class Man_VS_Cocktail : UIViewController{
@IBOutlet weak var Cocktail_Image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Cocktail_check_button(sender: AnyObject) {
var password_Text: UITextField?
let alertController = UIAlertController(title: "One more ticked off", message: "ask the barman to enter the password", preferredStyle: UIAlertControllerStyle.Alert)
let tickoff_action = UIAlertAction(title: "sign it off", style: UIAlertActionStyle.Default) {
action -> Void in
if let password = password_Text?.text{
print("password = \(password)")
if password == "pass123" {
self.Cocktail_Image.image = UIImage(named: "riddler_question_marks")
}
} else {
print("No password entered")
}
}
alertController.addTextFieldWithConfigurationHandler { (txtpassword) -> Void in
password_Text = txtpassword
password_Text!.secureTextEntry = true
password_Text!.placeholder = ""
}
alertController.addAction(tickoff_action)
self.presentViewController(alertController, animated: true, completion: nil)
}
作为附注,是否可以进行主休息动作,将所有图像重置为原始状态?我认为这将是一个if语句?
答案 0 :(得分:1)
这是使用NSDefaults的示例。您可以以更符合您需求的方式进行更改和格式化。
class Man_VS_Cocktail : UIViewController{
let defaults: NSUserDefaults
@IBOutlet weak var Cocktail_Image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Check saved password.
let passSaved = defaults.stringForKey("password")
if passSaved == "pass123" {
self.Cocktail_Image.image = UIImage(named: "riddler_question_marks")
} else {
// Set default image.
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Cocktail_check_button(sender: AnyObject) {
var password_Text: UITextField?
let alertController = UIAlertController(title: "One more ticked off", message: "ask the barman to enter the password", preferredStyle: UIAlertControllerStyle.Alert)
let tickoff_action = UIAlertAction(title: "sign it off", style: UIAlertActionStyle.Default) {
action -> Void in
if let password = password_Text?.text{
print("password = \(password)")
if password == "pass123" {
// Save the password
self.defaults.setObject(password_Text, forKey: "password")
// End save password
self.Cocktail_Image.image = UIImage(named: "riddler_question_marks")
}
} else {
print("No password entered")
}
}
alertController.addTextFieldWithConfigurationHandler { (txtpassword) -> Void in
password_Text = txtpassword
password_Text!.secureTextEntry = true
password_Text!.placeholder = ""
}
alertController.addAction(tickoff_action)
self.presentViewController(alertController, animated: true, completion: nil)
}
您甚至可以使用相同的格式检查AppDelegate。你甚至可以通过AppDelegate applicationWillTerminate添加一些延迟或清除保存的密码。