尝试使用UIalertcontroller在swift

时间:2016-02-03 22:46:22

标签: swift uialertcontroller

嗨,我对使用swift编码相当新,所以如果这是Noob问题,请耐心等待。

但是我想在UIalertcontroller检查密码是否正确后尝试让UIalertcontroller实现一个动作(更改图像)。

所以你按下按钮,弹出UIalertcontroller,你输入密码,密码是匹配的,它会改变图像。

是我想要它做的全部!

我遇到的问题是,我已将其编码并且有效,但无论文本字段放入何种文本,它都能正常工作。

我知道这应该是一件相对容易的事情,但是我已经在UIalertcontrollers上阅读过,这只是代码模糊。

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)") 
                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)

     }   
    }

如果有人能告诉我我做错了什么就是王牌!

1 个答案:

答案 0 :(得分:0)

您没有将密码与任何内容进行比较。您需要为视图添加一种方法来访问textField应匹配的密码,然后在此处进行比较:

if let password = password_Text?.text {
    print("Password == \(password)") 
    if password == "YOUR_PASSWORD_HERE" {
        self.Cocktail_Image.image = UIImage(named: "riddler_question_marks")
    }
}