I am using this code to implement a UITextField in my UIAlertView:
UIAlertView *receivedAlert = [[UIAlertView alloc] initWithTitle:message message:[NSString stringWithFormat:@"%@\n\n", [itemNameRows objectAtIndex:currentItem]] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil];
receivedField = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,25)];
receivedField.keyboardType = UIKeyboardTypeNumberPad;
receivedField.textAlignment = UITextAlignmentCenter;
receivedField.borderStyle = UITextBorderStyleRoundedRect;
receivedField.keyboardAppearance = UIKeyboardAppearanceAlert;
[receivedAlert addSubview:receivedField];
[receivedAlert show];
[receivedAlert release];
The problem arises when I have a message that is longer than 2 lines. The alert view resizes properly but the text field doesn’t move down to accommodate the longer line. Any ideas?
I can settle with shortening the message string if I have to.
You shouldn’t add subviews to UIAlertView. Apple’s docs explicitly prohibit it (“The view hierarchy for this class is private and must not be modified.”), and in addition to possibly getting you rejected, could also break in a future OS update (this happened once already around the iOS 3.1).
iOS 5 added alert view styles so that you can use text fields in alert views in a safe way. In addition to
UIAlertViewStyleDefault, there areUIAlertViewStyleSecureTextInput,UIAlertViewStylePlainTextInput, andUIAlertViewStyleLoginAndPasswordInput.Create an alert view and set the
alertViewStyleproperty to the appropriate value. You can then get the text field(s) by using-textFieldAtIndex:. Secure and plain text styles have a single text field at index 0, default has no text fields, and login and password have two text fields at indexes 0 (login) and 1 (password).See the UIAlertView Class Reference for more information.