【问题标题】:UITextView in UIAlertview not working in iOS 4.xUIAlertview 中的 UITextView 在 iOS 4.x 中不起作用
【发布时间】:2012-03-08 03:32:49
【问题描述】:

在我的医疗应用程序中,我有一个新功能,允许用户将 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 的 iPad 上运行它,相同的代码会创建一个警报视图,而文本字段只是丢失了。虽然我的应用程序适用于 iPhone,但我当然不希望 iPad 用户在运行它时遇到此错误。似乎iOS软件中可能存在错误。任何关于修复或替代的想法将不胜感激。提前致谢。

【问题讨论】:

    标签: iphone ios4 uitextfield uialertview


    【解决方案1】:

    试试这样:

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

    【讨论】:

    • 感谢您的帮助。您的解决方案确实可以根据需要显示文本输入字段。但是,光标随后会移出视野。我继续寻找使用 UITextField 而不是 UITextView 的解决方案,最后找到了一个可行的解决方案,使用更多代码:iphonedevelopment.blogspot.com/2009/02/…
    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 2018-03-16
    • 2011-03-25
    • 2014-01-11
    • 2014-07-27
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多