再次道歉,如果这是一个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语句?

最佳答案

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

10-08 09:32
查看更多