在IOS8以后取消了原来的UIAlertView和UIActionSheet,将其改为UIAlertController来一起处理,因为这两个空间极为相似,只不过一个是在中央,一个是下拉弹出菜单,因为给予了统一。
PS:并不是以上的两个控件不能使用了,还可以继续使用,只是Apple不在对其进行维护了。
统一后的用法是这样的:
let alertController = UIAlertController(title: "登录失败", message: "请检查网络是否打开", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "确认", style: .Cancel, handler: { (confirm) in
//目前还没有什么要处理的
})
alertController.addAction(alertAction)
self.presentViewController(alertController, animated: true, completion: nil)
首先声明一个UIAlertControlelr,在其构造方法中有一个参数 preferredStyle
这个参数可以选择:如Alert和ActionSheet。然后再声明一个UIAlertAction,使用controller将这个action添加到这个controller中。最后就是通过presentViewController这个方法将其呈现出来。
另外对于UIAlertView还可以在上面添加文本框之类的控件,使用方法如下:
UIAlertController.addTextFieldWithConfigurationHandler { (infor) in
}
也可以添加密码框,这个这里就不做出进一步的探讨了。