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 setTitle:@"Enter Name"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
UITextField * 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];
I also have the following code to deal with the alert view:
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%d", (int) buttonIndex);
if (buttonIndex == 1) { // OK pushed
NSLog([alert textField].text); // does not log anything
} else {
// Cancel pushed
}}
I also have a UIAlertViewExtended.h file that contains:
@class UITextField, UILabel;
@interface UIAlertView(Extended)
-(UITextField *)textField;
@end
My question is how do I get the text the user entered and how do I dismiss the keyboard?
Thanks,
Ben
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…