UIAlertView中的UITextView无法在iOS 4.x中工作

问题描述:

在我的医疗应用程序中,我有一项新功能,允许用户将PDF和其他资源下载到Documents文件夹以供离线查看。该应用程序支持iOS 4.1+和iOS 5.用户在警报视图中输入下载文件的名称。对于iOS 5,将文本字段放入警报现在很容易。对于类似的外观和感觉在iOS的4.x中,该代码通常会正常工作:UIAlertView中的UITextView无法在iOS 4.x中工作

 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]; 

我无奈从这个代码可以工作得很好了iOS 4.1的,4.2的事实推导和4.3的iPhone,以及如iPad上的iOS 4.1。但是,如果使用iOS 4.2或4.3在iPad中运行它,则相同的代码会创建一个警告视图,并且只会丢失文本字段。尽管我的应用程序适用于iPhone,但我当然不希望iPad用户运行它来遇到此错误。看起来iOS软件可能存在一个错误。任何想法修复或替代将非常感激。提前致谢。

尝试这样的:

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]; 
+1

感谢您的帮助。根据需要,您的解决方案的工作原理是显示文本输入字段。但是,光标漂移不见。我继续寻找使用UITextField而不是UITextView的解决方案,并最终找到了可以使用更多代码的解决方案:http://iphonedevelopment.blogspot.com/2009/02/alert-view-with-prompt.html – user183804 2012-03-10 16:57:17