在我的医疗应用程序中,我有一项新功能,允许用户将PDF和其他资源下载到Documents文件夹以供离线查看。该应用程序支持iOS 4.1+和iOS 5.用户在警报视图中输入下载文件的名称。对于iOS 5,现在可以轻松地在警报中放置文本字段。对于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];
我的沮丧源于这样一个事实:这个代码适用于iPhone上的iOS 4.1,4.2和4.3,以及iPad上的iOS 4.1。但是,如果在iOS 4.2或4.3中运行它,相同的代码会创建一个警告视图,文本字段就会丢失。虽然我的应用程序适用于iPhone,但我当然不希望运行它的iPad用户遇到此错误。似乎iOS软件中可能存在错误。任何修复或替代的想法将非常感激。提前谢谢。
答案 0 :(得分:3)
试试这样:
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];