gpt4 book ai didi

swift - 如何永久更改 ViewController 中的图像

转载 作者:行者123 更新时间:2023-11-30 10:07:19 26 4
gpt4 key购买 nike

如果这是一个菜鸟问题,但对于 swift 来说仍然相对较新,再次道歉,所以请耐心等待,

即使用户离开页面或关闭应用程序后,我也尝试更改 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 语句?

最佳答案

这是一个使用 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 清除保存的密码。

关于swift - 如何永久更改 ViewController 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com