Your application has presented a UIAlertController of style UIAlertControllerStyleActionSheet.

swift版本:3.0
Xcode版本:8.0

错误全文:

Your application has presented a UIAlertController of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller’s popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

错误原因:

在页面中设置了actionSheet类型的Alert,在iPhone环境可以正常显示,如下。但是在iPad环境下会报以上错误。原因是iPad上的actionSheet样式会直接转换为popover样式,如果不提前设置好popover样式的话,iPad就不知道如何显示,进而报错

错误源码:

    let optionMenu = UIAlertController(title: nil, message: "请设置头像", preferredStyle: .actionSheet)let getPictureFromLibraryButton = UIAlertAction(title: "打开图库", style: .default, handler: nil )let cancelButton = UIAlertAction(title: "取消", style: .cancel, handler: nil)optionMenu.addAction(getPictureFromLibraryButton)optionMenu.addAction(cancelButton)self.present(optionMenu, animated: true, completion: nil)

修正源码:

方法一:
 let optionMenu = UIAlertController(title: nil, message: "请设置头像", preferredStyle: .actionSheet)let getPictureFromLibraryButton = UIAlertAction(title: "打开图库", style: .default, handler: nil )let cancelButton = UIAlertAction(title: "取消", style: .cancel, handler: nil)optionMenu.addAction(getPictureFromLibraryButton)optionMenu.addAction(cancelButton)// support iPadoptionMenu.popoverPresentationController?.sourceView = self.viewoptionMenu.popoverPresentationController?.sourceRect = (tableView.cellForRow(at: indexPath)?.frame)!self.present(optionMenu, animated: true, completion: nil)</code>
方法二:
 let optionMenu = UIAlertController(title: nil, message: "请设置头像", preferredStyle: .actionSheet)let getPictureFromLibraryButton = UIAlertAction(title: "打开图库", style: .default, handler: nil )let cancelButton = UIAlertAction(title: "取消", style: .cancel, handler: nil)optionMenu.addAction(getPictureFromLibraryButton)optionMenu.addAction(cancelButton)      // support iPadoptionMenu.popoverPresentationController?.barButtonItem = self.saveBarButtomItemself.present(optionMenu, animated: true, completion: nil)
注意事项:

方法一和二必须选定一个
.sourceView 用来指定用来显示popover的view
.sourceRect 用来指定popover的箭头指向哪里, 参数是一个矩形。我的源码中是点一个tableView的Cell后弹出Alert,所以我这里传入的就是就是我所点击cell的Rect,效果如下

.barButtonItem只需指定按钮即可,剩下的事系统会自动完成,效果如下

iOS开发bug消灭之:Your application has presented a UIAlertController of style ...相关推荐

  1. IOS开发常见BUG和一些小技巧(PS:耐心看完,很实用)

    本文转载至 http://www.cnblogs.com/huayuan320/p/5777610.html [385][scrollView不接受点击事件,是因为事件传递失败] // // MySc ...

  2. [iOS]关于零基础学习iOS开发的学习方法总结

    关于零基础学习iOS开发的学习方法总结 最近很多零基础来参加蓝鸥培训的学生经常会问到一些学习方法的问题,就如下我自己见过的好的学习方法一起讨论一下. 蓝鸥iOS开发技术的学习路线图 程序员的主要工作是 ...

  3. iOS开发tips总结

    tip 1 :  给UIImage添加毛玻璃效果 func blurImage(value:NSNumber) -> UIImage { let context = CIContext(opti ...

  4. 最佳实践(2):iOS开发篇

    本文由 伯乐在线 - 戴仓薯 翻译,dopcn 校稿.未经许可,禁止转载! 英文出处:futurice.欢迎加入翻译组. 这份文档就像软件项目一样,如果我们不维护它就会逐渐腐坏.欢迎大家跟我们一起来维 ...

  5. iOS开发小技巧总结

    tip 1 :  给UIImage添加毛玻璃效果 func blurImage(value:NSNumber) -> UIImage { let context = CIContext(opti ...

  6. iOS开发常用三方库、插件、知名博客

    TimLiu-iOS iOS开发常用三方库.插件.知名博客等等,期待大家和我们一起共同维护,同时也期望大家随时能提出宝贵的意见(直接提交Issues即可). 持续更新... 版本:Objective- ...

  7. iOS开发第三方大全

    UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...

  8. iOS 开发的资源整理

    本文整理了常用的iOS.mac开源项目及类库,是开发者必备的一份资料,现在分享给广大的iOS程序员兄弟们,希望给他们的开发工作带来帮助. UI 下拉刷新 EGOTableViewPullRefresh ...

  9. iOS开发之资料收集

    github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自github:https://github ...

  10. iOS开发之开源项目库总汇

    iOS.mac开源项目及库汇总 UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅 ...

最新文章

  1. 联想 k2450 温度、耗电试验
  2. opencv实现分水岭算法
  3. 树形依赖背包dp的最优解问题
  4. Vlan中Trunk接口配置
  5. react.js app_如何创建Next.js入门程序以轻松引导新的React App
  6. Javascript乱弹设计模式系列(1) - 观察者模式(Observer)
  7. eclipse占用内存过大_idea被评最好用java开发工具,为什么用eclipse的人更多?
  8. Element ui tree树形控件获取当前节点id和父节点id
  9. java 引用类型 String_java基础之 数据类型 值传递 引用传递 String 四种引用类型...
  10. python如何将数据生成excel_Python如何将数据导出excel的技巧分享
  11. java 窗口组件的布局_Java 窗口 的常用布局
  12. Openwrt GCC 7.5编译sanitizer_internal_defs.h错误
  13. mac 上查看字体文件的路径
  14. echarts pie 饼图 border宽度
  15. IOT语义互操作性之本体论
  16. tolua++实现分析
  17. 导热电池粘合剂市场现状及未来发展趋势
  18. XPO学习(6)----实现一个简单的留言薄(ASP.NET)
  19. 通达OA使用手册(一)
  20. js 时间运算,时间加减

热门文章

  1. 【Cocos2d-x】物理引擎使用入门
  2. pytest 接口自动化 从百草园到三味书屋...
  3. MATLAB实现规范化(normalize)数据的函数实现
  4. Leetcode 853 车队
  5. python怎么打希腊字母_Python--画图时希腊字母的显示
  6. 无惧断电 小苏云“同城三机房”容灾演练成功
  7. asus路由器无线桥接模式设置
  8. 吉大计算机科学与技术网安,李嘉菲-吉林大学计算机科学与技术学院
  9. 安装CentOS找不到硬盘,无法分区,No disks selected
  10. CNCC 2022| 隐私计算:理论、技术、应用与未来