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
.
views:
8176answers:
8Try 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!)
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.
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
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.
dear rpetrich in your screenshot u have shown four buttons in two columns. how u achieved that ?? provide code example will be extra help
Explains how to set the number of columns, have not tested it.
http://iloveco.de/uikit-alert-types/
However there is a private method, setNumberOfRows:(int)n that will allow you to set a maximum number of rows to display the buttons in. To use this method we need to add our own additions to the UIAlertView class. We do this by adding an @interface for UIAlertView in our .m file.
// extend the UIAlertView class to remove the warning caused
// by calling setNumberOfRows.
@interface UIAlertView (extended)
- (void) setNumberOfRows:(int)num;
@end
This will allow us to call the method without the compiler throwing us a warning.
[myAlert setNumberOfRows:2];