在 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

        }];

谁能告诉我为什么会这样? inputViewUITextfield 属性在新的 UIAlertController View 中不可用吗?有没有人以这种方式成功使用 UIPickerView

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

我也收到来自文本字段的 UITextField Delegate 调用,所以我知道显示的是我创建的对象。当我清楚地将 pickerView 设置为其 inputView 时,我想不出标准键盘没有理由显示。

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

最佳答案

在我的头撞在 table 上一段时间后,我找到了解决方案,好吧,我的意思是我发现了我犯的愚蠢错误。

我正在向后进行 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:^{

        }];

关于uitextfield - iOS 8 在 UIActionSheet 中破坏了 UIPickerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25975974/

10-14 22:23
查看更多