之前我们介绍过AlertView和ActionSheet的用法,前者显示在页面中,而后者是从页面底部飞入的。IOS8中有个新的用法UIAlertController,用它就可以同时实现AlertView和ActionSheet,代码变得简便了很多。下面我们command进UIAlertController中看一下它的用法:
@availability(iOS, introduced=8.0) class UIAlertController : UIViewController { convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle) func addAction(action: UIAlertAction) var actions: [AnyObject] { get } func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) -> Void)!) var textFields: [AnyObject]? { get } var title: String? var message: String? var preferredStyle: UIAlertControllerStyle { get } }
enum UIAlertControllerStyle : Int { case ActionSheet case Alert }
var alertController = UIAlertController(title: "选择摇摇", message: nil, preferredStyle: .ActionSheet)//新写法 var priceAction = UIAlertAction(title: "价格", style: .Default, handler: nil) alertController.addAction(priceAction) var scoreAction = UIAlertAction(title: "评分", style: .Default, handler: nil) alertController.addAction(scoreAction) self.presentViewController(alertController, animated: true, completion: nil)
运行一下:
可以看到它依然在屏幕的底部弹出,现在我们保持代码不变,只把.ActionSheet换成.Alert,来看看效果:
怎么样,相比之前新建两个控制器,现在的用法是不是简单了很多?