【发布时间】:2015-09-28 13:24:41
【问题描述】:
Apple 已弃用 iOS 8.3 中的操作表。如何将操作表添加到我的用户界面?
我意识到 Apple 的文档对如何使用 UIAlertController 创建操作表不是很清楚。所以在玩了一会儿之后,我只想分享我的代码,因为我在 Stack Exchange 上找不到任何关于这个主题的有用信息。
【问题讨论】:
Apple 已弃用 iOS 8.3 中的操作表。如何将操作表添加到我的用户界面?
我意识到 Apple 的文档对如何使用 UIAlertController 创建操作表不是很清楚。所以在玩了一会儿之后,我只想分享我的代码,因为我在 Stack Exchange 上找不到任何关于这个主题的有用信息。
【问题讨论】:
我在我的 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];
然而,在 iOS 9 中,这一切只是让屏幕完全变暗,什么都不会出现。
是的……谢谢苹果。
解决方案是将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 样式的选项),则操作表将出现,但点击操作表之外的任意位置不会关闭菜单。
一个问题:
即使我们已经指定我们希望这个UIAlertController 具有UIAlertControllerStyleActionSheet 的样式,你需要将标题设置为nil,而不是一个空白字符串,否则你会在顶部得到一个难看的空白窗口。
我迫不及待地想看看 iOS 9.2 会破坏哪些完美运行的代码......
更新
我的评论:“然而,在 iOS 9 中,这一切只是让屏幕完全变暗,什么都不会出现”有点错误。
实际上,当屏幕键盘可见时,我正在打开我的UIActionSheet。在 iOS 9 中,键盘将出现在顶部您的UIActionSheet,因此您无法再看到您的操作表 (!!),但您确实看到了屏幕的其余部分变暗了。
使用UIAlertController,iOS 对用户更加友好,因为它在尝试在屏幕底部显示操作表之前隐藏屏幕键盘。究竟为什么 Apple 不对UIActionSheets 做同样的事情,我无法理解。
(叹气。)
我现在可以回去使用 Visual Studio 了吗?
另一个更新
Apple 表示 UIActionSheets 现在“在 iOS 8 中已弃用”。
但是,如果您想在包含文本框的 iOS 屏幕上继续使用它们,那么解决此错误、错误、问题的方法是在显示您的 UIActionSheet 之前添加一行代码:
[self.view endEditing:true];
这可确保在显示“已弃用”操作表之前关闭屏幕键盘..
(叹气。)如果有人需要我,我会在酒吧里。
【讨论】:
这是我在应用程序中用于操作表的代码的 sn-p,以防万一有人需要帮助以了解如何将 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];
【讨论】:
在 iOS 9 和 iPad 中,我必须添加一些重要的更改。将来可能会有人需要这个。
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Server"
message:@"Choose server to point to"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Development" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//code to run once button is pressed
}];
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Production" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//code to run once button is pressed
}];
[alert addAction:button1];
[alert addAction:button2];
UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = self.view;
//To present the actionsheet the bottom of screen
popPresenter.sourceRect = CGRectMake(self.createBtn.frame.origin.x, self.view.frame.size.height, self.createBtn.frame.size.width, self.createBtn.frame.size.height);
alert.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:alert animated:YES completion:nil];
【讨论】: