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];UITextField *_textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
alert.tag = 1000;
[_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
Post a Comment