如何根据密码比较输入的文本和字符串以触发新的序列

时间:2019-04-09 19:13:51

标签: ios swift

我有一个正在运行的应用程序,它的segue对它们有疑问,并将数据从一个segue传递到另一个segue。

我需要在我的最终按钮上添加一个密码,以触发将数据传递给最后一个视图控制器并将数据显示给拥有密码的人的事件。

我有将代码传递到最后一页的代码,我有将警报传递给用户输入密码的代码。

我的问题是,在加载警报页面之前,我是否有东西可以继续使用,是否输入密码并不重要。如果要在警报后继续进行搜索,则无论在文本框中输入什么,都不会发生任何事情。

@IBAction func DataReveal(_ sender: UIButton)
{
     ID = ID2.text
     date = date2.text
     Answer1 = answ1.text
     Answer2 = answ2.text
     switch sender.tag
     {
     case 1: send = "Thanks"
     default: print("No Selection")
     }
     let alertController = UIAlertController(title: "Password check", message: "Enter Password to see collected data", preferredStyle: .alert)
        alertController.addTextField
        {
            textField in
            textField.placeholder = "Enter Password"
            textField.isSecureTextEntry = true
        }
        let confirmActionBtn = UIAlertAction(title: "OK", style: .default)
        {
            [weak alertController] _ in
            guard let alertController = alertController, let textField = alertController.textFields?.first else
            {
                return
            }
            print("Password is \(String(describing: textField.text))")

            //Just compare Entered String in textfield with your original password password
            //if password is matched push or segue your required controller
        }
        alertController.addAction(confirmActionBtn)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        self.present(alertController, animated: true, completion: nil)
        self.performSegue(withIdentifier: "Link6", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "Link6", let destination = segue.destination as? DataviewController
        {
            destination.Answer2 = self.Answer2
            destination.Answer1 = self.Answer1
            destination.ID = self.ID
            destination.date = self.date
            destination.send = self.send
        }
    }

我已经按照提供的答案之一进行了更改,我尝试运行代码,但是仍然存在另一个问题,因为它仍然没有将文本字段与密码字符串变量以及密码进行比较输入的内容中没有任何进行动作的动作。

 @IBAction func DataReveal(_ sender: UIButton)
    {
        ID = ID2.text
        date = date2.text
        Answer1 = answ1.text
        Answer2 = answ2.text
        switch sender.tag
        {
        case 1: send = "current data collected"
        default: print("No Selection")
        }
        let alertController = UIAlertController(title: "Password check", message: "Enter Password to see collected data", preferredStyle: .alert)
        alertController.addTextField
        {
            textField in
            textField.placeholder = "Enter Password"
            textField.isSecureTextEntry = true
        }
        let confirmActionBtn = UIAlertAction(title: "OK", style: .default)
        {
            [weak alertController] _ in
            guard let alertController = alertController, let textField = alertController.textFields?.first else
            {
                return
            }
            print("Password is \(String(describing: textField.text))")
            func performSegue(withIdentifier identifier: String, sender: Any?) -> Bool
            {
                if identifier == "Link6"
                {
                    if (textField.text == self.password)
                    {
                        return true
                    }
                }
                return false
            }
        }
        alertController.addAction(confirmActionBtn)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        self.present(alertController, animated: true, completion: nil)
        //self.performSegue(withIdentifier: "Link6", sender: self)
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "Link6", let destination = segue.destination as? DataviewController
        {
            destination.Answer2 = self.Answer2
            destination.Answer1 = self.Answer1
            destination.ID = self.ID
            destination.date = self.date
            destination.send = self.send
        }
    }

2 个答案:

答案 0 :(得分:1)

这是通过shouldPerformSegue方法完成的。

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

    if identifier == "FirstToUsersSegue" {
        if (userText.text == username && passText.text == password) {
            return true
        }
    }

    return false
}

答案 1 :(得分:0)

警报与执行segue一样是异步的,因此此代码将执行segue 并显示警报。

如果您不希望在解除警报后才执行segue,则需要将调用移至performSegue(withIdentifier:sender:)处理程序内的confirmActionBtn