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
Anyone supporting iOS 5.0 onwards with ARC, there's this direct alertViewStyle property you can set:
-(void)showAlertWithTextField{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Name" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
// Change keyboard type
[[dialog textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[dialog show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1)
NSLog(@"%@",[[alertView textFieldAtIndex:0]text]);
}
For those who may care, here's a working solution:
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
Make sure you've created UITextField * nameField; in your .h file, then you can get at the text the user typed in by doing: inputText = [nameField text];
in the - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.
important note: for iOS 4.0+, the CGAffineTransform
is done automatically so you can leave that bit out if you're only targeting 4.0+. Otherwise, you will need to check which OS you are on and handle it accordingly.
A quite nice approach is, you can use the delegate methods of the UITextFieldDelegate to move the dialog up only when the keyboard is activated. See below.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -20);
[dialog setTransform: moveUp];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
CGAffineTransform moveDown = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveDown];
[textField resignFirstResponder];
return YES;
}
Found more clear solution. Check this code snippet out: Username and Password UITextFields in UIAlertView prompt