问题
In my medical app I have a new feature that allows the user to download PDF's and other resources to the Documents folder for offline viewing. The app supports iOS 4.1+ and iOS 5. The user enters a name for the downloaded file in an alert view. For iOS 5, placing a text field in an alert is now easy. For a similar look and feel in iOS 4.x, this code generally works well:
alertNameEntryOld = [[UIAlertView alloc] init];
[alertNameEntryOld setDelegate:self];
[alertNameEntryOld setTitle:@"Enter new file name:"];
[alertNameEntryOld setMessage:@" "];
[alertNameEntryOld addButtonWithTitle:@"Cancel"];
[alertNameEntryOld addButtonWithTitle:@"Continue"];
//Check current device orientation.
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
//Place text field position appropriate to device position.
if (interfaceOrientation == UIInterfaceOrientationPortrait)
alertTextFieldOld = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
alertTextFieldOld = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
alertTextFieldOld = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 30.0, 245.0, 25.0)];
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
alertTextFieldOld = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 30.0, 245.0, 25.0)];
[alertTextFieldOld setBackgroundColor:[UIColor whiteColor]];
//Auto-capitalize first letter with shift key enabled.
[alertTextFieldOld setAutocapitalizationType:UITextAutocapitalizationTypeSentences];
[alertNameEntryOld addSubview:alertTextFieldOld];
[alertNameEntryOld show];
[alertTextFieldOld release];
My frustration derives from the fact that this code works perfectly well for iOS 4.1, 4.2, and 4.3 on iPhone, as well as iOS 4.1 on iPad. However, if run it in iPad with iOS 4.2 or 4.3, the same code creates an alert view with the text field simply missing. Though my app is for iPhone, I certainly do not want iPad users running it to encounter this error. It seems that there may be a bug in the iOS software. Any thoughts for a fix or alternative would be much appreciated. Thanks in advance.
回答1:
Try like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter new file name:" message:@" " delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:@"Cancel", nil];
UITextView *mTextView = [[UITextView alloc] init];
[mTextView setFrame:CGRectMake(20.0, 30.0, 245.0, 25.0)];
[alert addSubview:mTextView];
[mTextView release];
[alert show];
[alert release];
来源:https://stackoverflow.com/questions/9612512/uitextview-in-uialertview-not-working-in-ios-4-x