ios – 在iPhone上的popover中显示UIAlertController ActionSheet
let alertController = UIAlertController(title: nil,message: nil,preferredStyle: .ActionSheet) alertController.modalPresentationStyle = .Popover // Add some buttons alertController.popoverPresentationController?.delegate = self alertController.popoverPresentationController?.barButtonItem = someBarButton self.presentViewController(alertController,animated: true,completion: nil)
这在iPad上工作正常,但在iPhone上alertController.popoverPresentationController为零.
我已经使用自适应segue风格成功地在iPhone上展示了Popovers在接口构建器中呈现为Popover,并实现adaptivePresentationStyleForPresentationController委托方法来返回正确的UIModalPresentationStyle,但是我被困在如何在UIAlertController的代码中执行,因为它在iPhone上没有popoverPresentationController
解决方法
alertController.modalPresentationStyle = .Popover alertController.popoverPresentationController!.sourceView = sender // CRASH!!! alertController.popoverPresentationController!.sourceRect = sender.bounds
有关如何实现iPhone Popover的所有细节(除了UIAlertController之外的其他内容),请查看我的iPhone Popover blog post.
popover演示控制器是零的事实是相当的说,它不应该是一个popover.
表视图替代
您可以将UITableViewController视为替代.使用分组风格实际上看起来相当不错的popovers.
选择器
您可能会一遍又一遍地遇到这个问题,您希望用户只需从几个选项中进行选择即可.我建议您将您要使用的任何用户界面控件封装到您自己的选择器对象中.可以从调用代码伪装其表视图或只是按钮的列表的实现细节,并且可以在特定索引处进行选择时具有委托或闭包回调.这大概是API的外观:
class Picker: UIViewController { init(items: [String]) selectionCompletion: (index: Int,item: String)->Void } // Usage: let picker = Picker(["Answer A","Answer B"]) picker.selectionCompletion = { index,item in // handle selection }
这样,您可以将其重用于任何您想要的API,而API非常简单
以上是来客网为你收集整理的ios – 在iPhone上的popover中显示UIAlertController ActionSheet全部内容,希望文章能够帮你解决ios – 在iPhone上的popover中显示UIAlertController ActionSheet所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。