iOS 9中的操作表问题

Der*_*ers 7 uialertcontroller ios9

Apple已弃用iOS 8.3中的操作表,如何向用户界面添加操作表?

我意识到Apple的文档不是很清楚如何用UIAlertController创建一个动作表.所以经过一段时间的游戏,我只是想分享我的代码,因为我无法在Stack Exchange上找到任何关于这个主题的有用信息.

Mik*_*ill 18

我在我的iPhone应用程序中遇到了同样的问题,UIActionSheet要求用户询问是否要拍照,或者从他们的图库中选择图像.

在此输入图像描述

在iOS 9之前,以下代码运行良好:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Take photo", @"Choose Existing", nil];
[actionSheet showInView:self.view];
Run Code Online (Sandbox Code Playgroud)

但是,在iOS 9中,所有这些都会使屏幕变暗,并且不会出现任何内容.

是的...谢谢Apple.

解决方案是UIActionSheet用a 替换UIAlertController.

UIAlertController* alert = [UIAlertController
                            alertControllerWithTitle:nil      //  Must be "nil", otherwise a blank title area will appear above our two buttons
                            message:nil
                            preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* button0 = [UIAlertAction
                          actionWithTitle:@"Cancel"
                          style:UIAlertActionStyleCancel
                          handler:^(UIAlertAction * action)
                          {
                                //  UIAlertController will automatically dismiss the view
                          }];

UIAlertAction* button1 = [UIAlertAction
                          actionWithTitle:@"Take photo"
                          style:UIAlertActionStyleDefault
                          handler:^(UIAlertAction * action)
                          {
                              //  The user tapped on "Take a photo"
                              UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
                              imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                              imagePickerController.delegate = self;
                              [self presentViewController:imagePickerController animated:YES completion:^{}];
                          }];

UIAlertAction* button2 = [UIAlertAction
                         actionWithTitle:@"Choose Existing"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             //  The user tapped on "Choose existing"
                             UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
                             imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                             imagePickerController.delegate = self;
                             [self presentViewController:imagePickerController animated:YES completion:^{}];
                         }];

[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

请注意,如果您包含取消选项(带有UIAlertActionStyleCancel样式的选项 ),则会显示操作表,但点击操作表外的任何位置都不会忽略该菜单.

一个问题:

尽管我们已经指定我们希望它UIAlertController具有样式UIAlertControllerStyleActionSheet,但您需要将标题设置为nil,而不是空字符串,否则您会在窗口顶部看到一个难看的间隙.

在此输入图像描述

我迫不及待地想看看iOS 9.2会破坏什么完美的代码......

更新

我的评论:" 但是,在iOS 9中,所有这些都会使屏幕变暗,并且没有任何内容会出现 ",这有点不对劲.

实际上,UIActionSheet当屏幕键盘可见时,我正在打开我.在iOS 9中,键盘将显示您的上方UIActionSheet,因此您无法再看到您的操作表(!!),但您确实看到屏幕的其余部分变暗.

使用时UIAlertController,iOS会更加用户友好,因为它会在尝试在屏幕底部显示操作表之前隐藏屏幕键盘.究竟为什么苹果不做同样的事情UIActionSheets超出了我.

(叹.)

请问我现在可以回到使用Visual Studio了吗?

另一个更新

Apple已经表示UIActionSheets现在已经"在iOS 8中被弃用".

但是,如果你想继续使用它们,在包含文本框的iOS屏幕上,那么解决这个bug,错误,问题的方法是在显示你的代码之前添加一行代码UIActionSheet:

[self.view endEditing:true]; 
Run Code Online (Sandbox Code Playgroud)

这样可以确保在显示"已弃用"操作表之前解除屏幕键盘的关闭.

(叹气.)如果有人需要我,我会在酒吧里.


Der*_*ers 5

这是我在我的应用程序中用于操作表的代码片段,以防万一需要帮助试图找出如何使用UIAlertController作为操作表.

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Action Sheet"
                                                                       message:@"This is an action sheet."
                                                                preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Button Title 1" style:UIAlertActionStyleDefault
                                                            handler:^(UIAlertAction * action) {
                                                                //code to run once button is pressed
                                                            }];
        UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Button Title 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //code to run once button is pressed
        }];

        [alert addAction:button1];
        [alert addAction:button2];
        [self presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)