iOS 8 在 UIActionSheet 中破坏了 UIPickerView [英] iOS 8 broke UIPickerView in UIActionSheet

查看:24
本文介绍了iOS 8 在 UIActionSheet 中破坏了 UIPickerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was presenting a picker in UIActionSheet very simply up until iOS 8. Now, it seems they have broken that capability with the latest update. It was never supported and the practice was discouraged in the docs all along.

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

Presumably the entire UIActionSheet capability is deprecated and will not be supported going forward. The documentation says to use UIAlertController instead.

我尝试切换到 UIAlertController 并且实际上发现我最初期望的将是一个比我最初提出的更好的解决方案.

I have tried switching to the UIAlertController and actually found what I initially expected would be a nicer solution than what I originally had presented.

我的原始代码:

// 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 中运行良好.

Worked fine in iOS 7.

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

My new code. Looks nicer but for some reason I cannot get the UITextField to respect my setting of the inputView property. It shows my text field with standard keyboard, otherwise this would be an acceptable alternative, better even than the original in my opinion.

新的,iOS 8 正在进行中:

New, work in progress for 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 成功?

Can anyone tell me why this is happening? Is inputView property of UITextfield unusable on the new UIAlertController view? Has anyone had success using UIPickerView in this manner?

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

I haven't included my code for the projectField or projectPicker properties but I have tested and checked. They do create valid objects. The picker is retained by my presenting VC and the text field is weak, owned by the AlertViewController I assume. I tried retaining it as well, that had no effect.

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

I do receive UITextField Delegate calls from the text field as well, so I know that it is the object that I created that is presented. I can think of no reason for the standard keyboard to show when I have clearly set the pickerView as its inputView.

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

See resulting view in screenshot:

推荐答案

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

After banging my head on the desk for some time, I found the solution, well, I mean I found the dumb error I was making.

我正在反向进行 textField 配置.我以为目的是将 textField 参数传递给我的文本字段,而我应该设置给定文本字段的属性.

I was doing the textField configuration backwards. I thought the intent was to pass the textField parameter my text field, when I should have been setting the properties of the text field given.

只需更改为

// 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:^{

        }];

这篇关于iOS 8 在 UIActionSheet 中破坏了 UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆