2016-02-10 46 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添加某種延遲或清除保存的密碼。