Question

How do I add a textfield to the alertview? I'm trying to do an app where before the app commits the editing, the user must authenticate 1st by typing his/her password on the said alertview but how can I do this? It seems like the code I've searched doesn't work anymore. Here it is:

  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;

the error said

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

I also would like to ask how can I put codes on the Confirm button on the alertview.

Était-ce utile?

La solution

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

or if you need it to be secure (for passwords)

alert.alertViewStyle = UIAlertViewStyleSecureTextInput;

Edit:

I'm not 100% sure what you mean by "put codes on the confirm button" but if you want to know whether they pushed confirm or cancel, you only need to implement

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //check the button index and do something if it's the right one.
}

Autres conseils

Adding a textField into an alert view (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)

To check the data in the checkBoxes just add this in your "OK" Action handler:

let passcodeEntered:String = (pincodeAlert.textFields?.first as UITextField).text
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top