1

如何将文本字段添加到警报视图?我正在尝试做一个应用程序,在应用程序提交编辑之前,用户必须通过在所述警报视图上输入他/她的密码来进行第一次身份验证,但我该怎么做呢?我搜索的代码似乎不再起作用了。这里是:

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In" message:@"Please enter your Password" delegate:self cancelButtonTitle:@"Log In" otherButtonTitles:@"Cancel", nil];
    [alert addTextFieldWithValue:@"" label:@"Password"];
    UITextField *tf = [alert textFieldAtIndex:0];
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.keyboardType = UIKeyboardTypeAlphabet;
    tf.keyboardAppearance = UIKeyboardAppearanceAlert;
    tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
    tf.autocapitalizationType = UITextAutocorrectionTypeNo;

错误说

 "No visible @interface for 'UIAlertView' 
 declares the selector 'addTextFieldWithValue:label:'" 
 on the [alert addTextFieldWithValue:@"" label:@"Password"];

我还想问一下如何将代码放在警报视图的确认按钮上。

4

2 回答 2

9
alert.alertViewStyle = UIAlertViewStylePlainTextInput;

或者如果您需要它是安全的(用于密码)

alert.alertViewStyle = UIAlertViewStyleSecureTextInput;

编辑:

我不是 100% 确定您所说的“将代码放在确认按钮上”是什么意思,但如果您想知道他们是按确认还是取消,您只需要实施

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //check the button index and do something if it's the right one.
}
于 2013-01-31T02:32:58.153 回答
0

将 textField 添加到警报视图(Swift)中:

    let pincodeAlert:UIAlertController = UIAlertController(title: "Hello", message: "Enter a new passcode", preferredStyle: UIAlertControllerStyle.Alert)
           pincodeAlert.addTextFieldWithConfigurationHandler({ (pinCodeTextField:UITextField!) -> Void in
            pinCodeTextField.placeholder = "password"
            pinCodeTextField.secureTextEntry = true
           })
    //Add the textField       
    pincodeAlert.addTextFieldWithConfigurationHandler({ (pinCodeTextField2:UITextField!) -> Void in
            pinCodeTextField2.placeholder = "Confirm password"
            pinCodeTextField2.secureTextEntry = true
           })

    pincodeAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
                //check entered passcodes
                }))
                pincodeAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
                presentViewController(pincodeAlert, animated: true, completion: nil)

要检查复选框中的数据,只需将其添加到您的“OK”操作处理程序中:

let passcodeEntered:String = (pincodeAlert.textFields?.first as UITextField).text
于 2015-01-08T11:56:51.857 回答