I placed a UITextField into a UIAlertView and moved it up so the keyboard wouldn\'t cover it up with the following code:
[dialog setDelegate:self];
[dialog s
It's worth checking out
http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html?cmd=success#comment3870
For a complete and comprehensive solution.
This is for iOS5 and ARC.
-(void)showAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Album"
message:@"Enter a name for the album."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* textfield = [alertView textFieldAtIndex:0];
textfield.placeholder = @"Title";
[alertView show];
}
-(void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 1) {
NSLog(@"Cancel");
return;
}
UITextField* textfield = [alertView textFieldAtIndex:0];
NSLog(@"Save. text: %@", textfield.text);
}
A simple way to locate the text field, without keeping an explicit reference to it, is to set its tag:
nameField.tag = ALERTVIEW_NAMEFIELD;
Make sure it is different from 0 and from other UIView
object tags you may have, including the parent dialog!
Then inside the alertView:clickedButtonAtIndex:
handler, you can retrieve your text field this way:
UITextField *nameField = (UITextField *)[alertView viewWithTag:ALERTVIEW_NAMEFIELD];
Would like to add one minor tweak to iWasRobbed's answer:
GenericAlertDialogs.m:
+ (void)displayInputDialog:(NSString*)title delegate:(id<UIAlertViewDelegate>)delegate textFiled:(UITextField*)txtField
{
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:delegate];
[dialog setTitle:title];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
{
txtField.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
}
else
{
txtField.frame = CGRectMake(20.0, 30.0, 245.0, 25.0);
}
[txtField becomeFirstResponder];
[txtField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:txtField];
[dialog show];
}
Some other .m file (implements UIAlertViewDelegate)
self->txtChangeName = [[UITextField alloc] init];
[GenericAlertDialogs displayInputDialog:@"Some title...:"
delegate:self
textFiled:txtChangeName];
The UIAlertViewDelegate protocol handling:
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
if([txtChangeName.text length] > 0)
{
self->userInput = [txtChangeName text];
}
}
self->txtChangeName = nil;
}
iOS 4+ compliant.
This is actually only 1 more line to the regular UIAlertView code. Hope this helps!
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:@"This is a alert with a textfield" delegate:self cancelButtonTitle:@"YAY" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];
only runs on iOS 5 or later
Check out this implementation: https://github.com/josecastillo/EGOTextFieldAlertView