从UITextField中获取任何东西

时间:2016-02-26 06:07:58

标签: ios xcode swift uitextfield

在下面的代码中,我有一个按钮,提示用户输入我的管理密码。如果我单击“确定”按钮,它应该能够将消息设置为 inputTextField 并显示消息

image

var inputTextField: UITextField?

@IBAction func logOutButton(sender: AnyObject) {
    let actionController = UIAlertController(title: "Log out", message: "Are you sure you want to log out?", preferredStyle: UIAlertControllerStyle.Alert)

    actionController.addTextFieldWithConfigurationHandler { (textField) in
        textField.placeholder = "Admin passphrase"
        textField.secureTextEntry = true

        self.inputTextField = textField

    }

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){UIAlertAction in

        if self.inputTextField != nil{
            print(self.inputTextField)
        }else{
            print("Nothing")
        }

        self.dismissViewControllerAnimated(true, completion: nil)
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default){UIAlertAction in
        print("Test: Log out action cancelled")
    }



    actionController.addAction(okAction)
    actionController.addAction(cancelAction)
    self.presentViewController(actionController, animated: true, completion: nil)
}

这是我在输入“admin”后从控制台获得的,然后单击“确定”

控制台消息: 可选(< _UIAlertControllerTextField:0x7fc90bc58d00; frame =(4 4; 229 16); text ='admin'; clipsToBounds = YES; opaque = NO; gestureRecognizers =; layer =>)

一切正常,直到我将以下内容改为

var inputTextField: String?

self.inputTextField = textField.text

结果: 可选( “”)

我确定必须将某些内容发送到inputTextField但是我无法理解它,任何想法?

3 个答案:

答案 0 :(得分:2)

您也可以通过以下方式访问UIAlertController的textField:

let alertView = UIAlertController(title: "title", message: "Type the msg", preferredStyle: UIAlertControllerStyle.Alert)

alertView.addTextFieldWithConfigurationHandler { (textField) -> Void in
    textField.placeholder = "Admin passphrase"
    textField.secureTextEntry = true
}

let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (alertAction) -> Void in

    let val = (alertView.textFields![0] as UITextField).text! as String
    if val == "" {
        print("nothing")
    } else {
        print(val)
    }
}
alertView.addAction(alertAction)

presentViewController(alertView, animated: true, completion: nil)

答案 1 :(得分:1)

只需在OK按钮功能中使用它。

//  myText will be the first textField in alert controller, store its text into ans
let myText = actionController.textFields![0] 
let ans = myText.text
if ans != ""{
    print(ans)
}else{
    print("Nothing")
}

这将获得你的alertController中的第一个textField,然后你只需将它存储在任何变量中,你是否有条件。

答案 2 :(得分:0)

您需要从prombt alertview获取文本。使用以下代码。

        let actionController = UIAlertController(title: "Log out", message: "Are you sure you want to log out?", preferredStyle: UIAlertControllerStyle.Alert)
            actionController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "Admin passphrase"
            textField.secureTextEntry = true

            //self.inputTextField = textField // remove

        }

        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){UIAlertAction in

            if self.inputTextField != nil{
                let textField:UITextField = (actionController.textFields?[0])!;
                self.inputTextField.text = textField.text;
                 NSLog("%@",textField.text!)
            }else{
                print("Nothing")
            }

            self.dismissViewControllerAnimated(true, completion: nil)
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default){UIAlertAction in
            print("Test: Log out action cancelled")
        }

        actionController.addAction(okAction)
        actionController.addAction(cancelAction)
        self.presentViewController(actionController, animated: true, completion: nil)