Adding an UITextField to an UIAlertView in iPhone / iOS application


As we all know, UIAlertView is subclassed from UIView, its possible to add sub views to the alert view.

Today, we will try to add an text field to UIAlertView. The code is attached below.


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"See the text field below"     message:@"this gets covered!\n"  delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"OK", nil]; 
    alert.tag = 1000;
         UITextField *_textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    [_textField setPlaceholder:@"Place holder text"];
    _textField.tag = 100;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;    [_textField setBackgroundColor:[UIColor whiteColor]];    [_textField becomeFirstResponder];    [alert addSubview: _textField];
    [_textField release];
    [alert show];
    [alert release];

And to get the value of the text field, use the viewWithTag parameter to identify the text field.


- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {       



UITextField *_textField = (UITextField *) [actionSheet viewWithTag:1];        NSLog(@"Entered text in alert view:  <%@>", [_textField text]);}

In the same manner, we can add UIImageView to alert view.

Comments

Popular posts from this blog

Integrating ZXing QR Code reader in iPhone / iOS applications

Multiple line of text in UIPickerView

Connect Samsung devices to Kies on Mac