2016-02-10 143 views
0

道歉,如果这是一个noobie问题,但还是相对较新的迅速,所以请多多包涵,我怎样才能改变形象的UIViewController permantly再次

我试图改变一个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语句?

回答

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添加某种延迟或清除保存的密码。