【问题标题】:iOS 8 broke UIPickerView in UIActionSheetiOS 8 在 UIActionSheet 中破坏了 UIPickerView
【发布时间】:2014-09-22 14:00:29
【问题描述】:

在 iOS 8 之前,我在 UIActionSheet 中非常简单地展示了一个选择器。现在,他们似乎已经通过最新更新打破了该功能。它从未得到支持,并且在文档中一直不鼓励这种做法。

大概整个UIActionSheet 功能已被弃用,今后将不再受支持。文档说要改用 UIAlertController。

我尝试切换到UIAlertController,实际上发现我最初期望的解决方案比我最初提出的解决方案更好。

我的原始代码:

// Ask user for project to associate the new issue with a project
        UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Select Project", @"Title for Action Sheet")
                                                          delegate:self 
                                                 cancelButtonTitle:klocalized_CancelButtonTitle
                                            destructiveButtonTitle:nil 
                                                 otherButtonTitles:klocalized_SelectButtonTitle ,nil];

        [menu addSubview:self.projectPicker];
        [menu showFromTabBar:self.tabBarController.tabBar];
        [menu setBounds:CGRectMake(0, 0, 320, 700)];

在 iOS 7 中运行良好。

我的新代码。看起来更好,但由于某种原因,我无法让 UITextField 尊重我对 inputView 属性的设置。它使用标准键盘显示我的文本字段,否则这将是一个可以接受的替代方案,在我看来甚至比原来的更好。

iOS 8 的新功能正在开发中:

// is iOS 8, ActionSheet, newly deprecated, will not allow subviews. Use UIAlertController instead
        NSString *title = NSLocalizedString(@"Select Project", @"Title for Action Sheet");
        UIAlertController *projectSelector = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
        [projectSelector addAction:[UIAlertAction actionWithTitle:klocalized_CancelButtonTitle style:UIAlertActionStyleCancel handler:nil]];
        [projectSelector addAction:[UIAlertAction actionWithTitle:klocalized_SelectButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            // Do my button action stuff here

        }]];

        [projectSelector addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            // projectField is lazily created property.
            //My picker is set as its inputView but standard keyboard is always presented instead.
            textField = self.projectField;
        }];

        [self presentViewController:projectSelector animated:YES completion:^{
            // Do my final work here

        }];

谁能告诉我为什么会这样? UITextfieldinputView 属性在新的 UIAlertController 视图上是否不可用?有没有人以这种方式成功使用UIPickerView

我没有包含 projectFieldprojectPicker 属性的代码,但我已经测试和检查过。他们确实创建了有效的对象。选择器由我呈现的 VC 保留,文本字段很弱,由我假设的 AlertViewController 拥有。我也尝试保留它,但没有效果。

我也收到来自文本字段的UITextField 代表调用,所以我知道呈现的是我创建的对象。当我将pickerView明确设置为inputView时,我想不出标准键盘显示的理由。

在屏幕截图中查看结果视图:

【问题讨论】:

    标签: uitextfield uialertview uipickerview inputview uialertcontroller


    【解决方案1】:

    在我的头在桌子上敲了一段时间之后,我找到了解决方案,嗯,我的意思是我发现了我犯的愚蠢错误。

    我正在向后进行 textField 配置。我认为意图是在我应该设置给定文本字段的属性时将 textField 参数传递给我的文本字段。

    直接改成

    // is iOS 8, ActionSheet, newly deprecated, will not allow subviews. Use UIAlertController instead
            NSString *title = NSLocalizedString(@"Select Project", @"Title for Action Sheet");
            UIAlertController *projectSelector = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
            [projectSelector addAction:[UIAlertAction actionWithTitle:klocalized_CancelButtonTitle style:UIAlertActionStyleCancel handler:nil]];
            [projectSelector addAction:[UIAlertAction actionWithTitle:klocalized_SelectButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
            }]];
    
            [projectSelector addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                //Set the properties of the textField provided. DO NOT CREATE YOUR OWN.
                [textField setPlaceholder:NSLocalizedString(@"Make Selection", @"PlaceHolder for project field when awaiting picker choice")];
                [textField setInputView:self.projectPicker];
                [textField setDelegate:self];
                self.projectField = textField;
            }];
    
            [self presentViewController:projectSelector animated:YES completion:^{
    
            }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多