[EDIT] Hmm. Perhaps this question should be titled \"what is the default user-input dialog view called in CocoaTouch?\" I realize that I can create an entire view that is
Most probably You would want to look into the addTextFieldWithValue
method of the UIAlertView
? Add the following code somewhere at the top of Your class:
@interface UIAlertView ()
- (void) addTextFieldWithValue: (NSString*) val label: (NSString*) label;
- (UITextField*) textField;
@end
It’s not official, but IMHO it’s not getting You rejected from the App store and it’s much better solution than hacking the textfield into the dialog Yourself.
https://github.com/TomSwift/TSAlertView
This library actually creates the control from scratch rather than attempting to hack UIAlertView, which is generally a Bad Plan (TM)
Zoul proposed the best method, to capture user input just do:
a) Add the UITextFieldDelegate protocol to your class.
b) Do something like
UIAlertView *insertScore = [UIAlertView new];
[insertScore setDelegate:self];
[insertScore setTitle:@"New Title!"];
[insertScore addButtonWithTitle:@"Cancel"];
[insertScore addButtonWithTitle:@"Ok"];
insertScore.message = @"\n";
[insertScore addTextFieldWithValue:@"Input" label:@"player"];
[[insertScore textField] setDelegate:self];
[insertScore show];
[insertScore release];
c) The crucial part was to set the delegate of the textField to self, then to access data you can simply:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@",[[alertView textField] text]);
}
Hope this helps someone, since I had to think a bit to get it right.
Try putting in some (\n)s after the title in the UIAlertView initialization. That will push down the buttons. And I agree with Stephen here. There are chances that Apple might reject an app if it uses controls in a way they shouldn't be. (there's some clause in the Human Interface Guidelines about that!)
Even if you can get this working it's not going to be very iPhone-y. The UIAlertView
really is not designed for user input like this. If you look in all the Apple apps you'll see that they use a new view that displayed using the presentModalViewController:
method of UIViewController
.
Edit: This advice is no longer as true as it was when I wrote it. Apple have increasingly used alert views as text entry boxes and iOS5 even includes native support without having to mess around with views (check out the alertViewStyle property).
I think maybe if you need to have four buttons then using a custom UIViewController
is probably still the right way to go. But if you just want to enter a password with OK/Cancel buttons then it's fine.
This simpler method works for me:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
message:@"<Alert message>" delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert addTextFieldWithValue:@"" label:@"Text Field"];
Hope that helps. Oh if you needed multiple button rows then it's:
[alert setNumberOfRows:3];
Cheers