iOS 9中的操作表问题 [英] Action Sheet problems in iOS 9

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

问题描述

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

Apple has deprecated the action sheet in iOS 8.3 how do you add an action sheet to my user interface?

我意识到Apple的文档对于如何使用UIAlertController创建操作表并不十分清楚.因此,经过一番闲逛之后,我只想分享我的代码,因为我在Stack Exchange上找不到关于此主题的任何有用信息.

I realized Apple's documentation isn't very clear on how to create an action sheet with UIAlertController. So after a little playing around I just wanted to share my code, since I couldn't really find anything useful on Stack Exchange about this topic.

推荐答案

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

I had the same problem in my iPhone app, with an UIActionSheet to ask the user if they wanted to take a photo, or pick an image from their gallery.

在iOS 9之前,以下代码可以正常工作:

Prior to iOS 9, the following code worked fine:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Take photo", @"Choose Existing", nil];
[actionSheet showInView:self.view];

但是,在iOS 9中,所有这些操作完全使屏幕变暗,并且什么也不会出现.

However, in iOS 9, all this does is completely darken the screen, and nothing would appear.

是的,谢谢苹果.

解决方案是将UIActionSheet替换为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];

请注意,如果您包括取消选项(样式为UIAlertActionStyleCancel的选项),则将显示操作表,但请点击操作表之外的任何位置不会关闭菜单.

Notice that if you don't include a cancel option (an option with the UIAlertActionStyleCancel style), then the action sheet will appear, but tapping anywhere outside of the action sheet won't dismiss the menu.

一个陷阱:

即使我们已经指定我们希望此UIAlertController具有样式UIAlertControllerStyleActionSheet,您仍需要将标题设置为nil,而不是空白字符串,否则顶部会出现难看的间隙的窗口.

Even though we've specified that we want this UIAlertController to have the style UIAlertControllerStyleActionSheet, you need to set the title as nil, rather than a blank string, otherwise you get an ugly gap at the top of the window.

我迫不及待地想看到完美运行的代码iOS 9.2会破解...

I can't wait to see what perfectly-working code iOS 9.2 will break...

更新

我的评论:"但是,在iOS 9中,所有这些操作完全使屏幕变暗,什么也不会出现".

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

Actually, I was opening my UIActionSheet while the onscreen keyboard was visible. And in iOS 9, the keyboard will appear on top of your UIActionSheet, so you can no longer see your action sheet (!!), but you do see that the rest of the screen has turned darker.

使用UIAlertController,iOS更加用户友好,因为它会在尝试在屏幕底部显示操作表之前将其隐藏在屏幕键盘上.正是为什么Apple不对UIActionSheets做同样的事情超出了我的理解.

With UIAlertController, iOS is slightly more user-friendly, as it hides the onscreen keyboard before attempting to display the action sheet at the bottom of the screen. Exactly why Apple doesn't do the same with UIActionSheets is beyond me.

(叹气)

请现在我可以重新使用Visual Studio吗??

Please may I go back to using Visual Studio, now..?

另一个更新

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

Apple has said that UIActionSheets are now "deprecated in iOS 8".

但是,如果要继续使用它们,请在包含文本框的iOS屏幕上,然后针对此错误errr问题的解决方法是在显示UIActionSheet之前添加一行代码:

However, if you want to keep using them, on your iOS screens which contain textboxes, then a workaround to this bug, errr, problem, is to add one line of code before displaying your UIActionSheet:

[self.view endEditing:true]; 

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

This makes sure the onscreen keyboard is dismissed before displaying your "deprecated" action sheet..

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

(Sigh.) If anyone needs me, I'll be in the pub.

这篇关于iOS 9中的操作表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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