Jamshed Alam Jamshed Alam - 1 month ago 8
Objective-C Question

Created 3 UITextField in UIAlertView but not getting action

I have created an UIAlertView with three (3) UITextField. How i can get the action of UIAlertView. The clickedButtonAtIndex delegate method is working.

But not getting the text i input in the field. Any idea ? Thanks in advance.

UIView *v_alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
v_alert.tag=10;

UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Your Previous Password";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;

[v_alert addSubview:textField1];

UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Your New Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;

[v_alert addSubview:textField2];


UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;

[v_alert addSubview:textField3];


alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertV setValue:v_alert forKey:@"accessoryView"];
alertV.tag==100;
alertV.delegate=self;

[alertV show];


In delegate :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex");



UITextField*txt1=[[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:0];
UITextField*txt2= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:1];
UITextField*txt3= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:2];

NSLog(@"%@--%@--%@",txt1.text,txt2.text,txt3.text);
}


enter image description here

Answer

Declare Your 3 TextField

UITextField *textField1, *textField2, *textField3; 

Then replace this code with your code.

 UIView *alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
    alertview.backgroundColor = [UIColor whiteColor];
    alertview.tag=10;
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.placeholder = @"Your Previous Password";
    textField1.secureTextEntry = YES;
    textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
    textField1.delegate = self;
    textField1.tag=1;

    [alertview addSubview:textField1];

    textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
    textField2.placeholder = @"Your New Password";
    textField2.secureTextEntry = YES;
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
    textField2.delegate = self;
    textField2.tag=2;

    [alertview addSubview:textField2];


    textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
    textField3.placeholder = @"Email";
    textField3.borderStyle = UITextBorderStyleRoundedRect;
    textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
    textField3.delegate = self;
    textField3.tag=3;

    [alertview addSubview:textField3];


    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
    [alertV setValue:alertview  forKey:@"accessoryView"];
    alertV.tag==100;
    alertV.delegate=self;

    [alertV show];

Now your UIAlertView Button Method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"Button No click");
    }
    if (buttonIndex == 1) {
         NSLog(@"Button Yes Cancel");
    }
    NSLog(@"%@--%@--%@",textField1.text,textField2.text,textField3.text);
}

Output : Gems--Moradiya--Himmoradiya@gmail.com