如何将文本字段添加到警报视图? Xcode 4.5 iOS6

时间:2013-01-31 02:26:05

标签: ios xcode alertview

如何在alertview中添加文本字段?我正在尝试做一个应用程序,在应用程序提交编辑之前,用户必须通过在所述alertview上键入他/她的密码来验证第一,但我该怎么做?似乎我搜索的代码不再起作用了。这是:

  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"];

我还想问一下如何将代码放在alertview上的Confirm按钮上。

2 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分: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)

要检查复选框中的数据,只需将其添加到"确定"动作处理程序:

let passcodeEntered:String = (pincodeAlert.textFields?.first as UITextField).text