尝试使用UIalertcontroller验证swift中的操作

尝试使用UIalertcontroller验证swift中的操作,swift,uialertcontroller,Swift,Uialertcontroller,您好,我是一个相当新的编码与swift所以请容忍我,如果这是一个Noob的问题 但是我试图让UIalertcontroller在检查密码是否正确后执行操作(更改图像) 所以你按下按钮,UIalertcontroller弹出,你输入密码,密码是匹配的,它改变了图像 这就是我想要它做的一切 我的问题是,我已经对它进行了编码,它可以工作,但不管文本字段中输入了什么文本,它都可以工作 我知道这应该是一件相对容易的事情,但我已经读了很多关于UIAlertController的mch文章,它只是一个模糊的代

您好,我是一个相当新的编码与swift所以请容忍我,如果这是一个Noob的问题

但是我试图让UIalertcontroller在检查密码是否正确后执行操作(更改图像)

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

这就是我想要它做的一切

我的问题是,我已经对它进行了编码,它可以工作,但不管文本字段中输入了什么文本,它都可以工作

我知道这应该是一件相对容易的事情,但我已经读了很多关于UIAlertController的mch文章,它只是一个模糊的代码

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)

     }   
    }

如果有人能告诉我我做错了什么,那就太好了

您没有将密码与任何东西进行比较。您需要为视图添加一种方式来访问文本字段应匹配的密码,然后在此处进行比较:

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