UIAlertController 同時(shí)替代了 UIAlertView 和 UIActionSheet,從系統(tǒng)層級(jí)上統(tǒng)一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示。
UIAlertController 是 UIViewController 的子類,而非其先前的方式。因此新的 alert 可以由 view controller 展示相關(guān)的配置中獲益很多。
UIAlertController 不管是要用 alert 還是 action sheet 方式展示,都要以 title 和 message 參數(shù)來(lái)初始化。Alert 會(huì)在當(dāng)前顯示的 view controller 中心以模態(tài)形式出現(xiàn),action sheet 則會(huì)在底部滑出。Alert 可以同時(shí)有按鈕和輸入框,action sheet 僅支持按鈕。
新的方式并沒(méi)有把所有的 alert 按鈕配置都放在初始化函數(shù)中,而是引入了一個(gè)新類 UIAlertAction 的對(duì)象,在初始化之后可以進(jìn)行配置。這種形式的 API 重構(gòu)讓對(duì)按鈕數(shù)量、類型、順序方便有了更大的控制。同時(shí)也棄用了 UIAlertView 和 UIActionSheet 使用的delegate 這種方式,而是采用更簡(jiǎn)便的完成時(shí)回調(diào)。
新舊Alert 方式對(duì)比
標(biāo)準(zhǔn)的Alert樣式

舊方法 :UIAlertView
let alertView = UIAlertView(title: "Default Style", message: "A standard alert.", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
alertView.alertViewStyle = .Default
alertView.show()
// MARK: UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
// ...
}
}
方法:UIAlertController
let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
標(biāo)準(zhǔn)的 Action Sheet 樣式

UIActionSheet
let actionSheet = UIActionSheet(title: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Destroy", otherButtonTitles: "OK")
actionSheet.actionSheetStyle = .Default
actionSheet.showInView(self.view)
// MARK: UIActionSheetDelegate
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
...
}
}
UIAlertController
let alertController = UIAlertController(title: nil, message: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in
println(action)
}
alertController.addAction(destroyAction)
self.presentViewController(alertController, animated: true) {
// ...
}
新功能
UIAlertController 并不只是對(duì)已有的 API 做了清理,而是進(jìn)行了標(biāo)準(zhǔn)化歸納。以前,預(yù)設(shè)的樣式閑置有很多(swizzling 雖然可以提供更多的功能但還是有很大風(fēng)險(xiǎn))。UIAlertController 讓以前看起來(lái)很神奇的事情變?yōu)榱丝赡堋?/p>
UIAlertController is not just a cleanup of pre-existing APIs, it's a generalization of them. Previously, one was constrained to whatever presets were provided (swizzling in additional functionality at their own risk). With UIAlertController, it's possible to do a lot more out-of-the-box:
帶有警示按鈕的Alert

這種行為已經(jīng)被 UIAlertActionStyle 所涵蓋,共有三種類型:
- Default: 對(duì)按鈕應(yīng)用標(biāo)準(zhǔn)樣式。
- Cancel: 對(duì)按鈕應(yīng)用取消樣式,代表取消操作不做任何改變。
- Destructive: 對(duì)按鈕應(yīng)用警示性的樣式,提示用戶這樣做可能會(huì)改變或刪除某些數(shù)據(jù)。
所以想要對(duì)模態(tài)的 alert 加一個(gè)警示性的按鈕,只需要加上 .Destructive 風(fēng)格的 UIAlertAction 屬性:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
println(action)
}
alertController.addAction(cancelAction)
let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in
println(action)
}
alertController.addAction(destroyAction)
self.presentViewController(alertController, animated: true) {
// ...
}
大于2個(gè)按鈕的Alert

有 1 個(gè)或者 2 個(gè)操作的時(shí)候,按鈕會(huì)水平排布。更多按鈕的情況,就會(huì)像 action sheet 那樣展示:
let oneAction = UIAlertAction(title: "One", style: .Default) { (_) in }
let twoAction = UIAlertAction(title: "Two", style: .Default) { (_) in }
let threeAction = UIAlertAction(title: "Three", style: .Default) { (_) in }
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in }
alertController.addAction(oneAction)
alertController.addAction(twoAction)
alertController.addAction(threeAction)
alertController.addAction(cancelAction)
創(chuàng)建登錄表單

iOS 5 就為 UIAlertView 加入了 alertViewStyle 屬性,將原本私有的 API 暴露出來(lái)給開發(fā)者使用 —— 像某些系統(tǒng)內(nèi)建應(yīng)用一樣允許在 alert 中顯示登錄和密碼框。
在 iOS 8 中,UIAlertController 則加入了 addTextFieldWithConfigurationHandler 方法:
let loginAction = UIAlertAction(title: "Login", style: .Default) { (_) in
let loginTextField = alertController.textFields![0] as UITextField
let passwordTextField = alertController.textFields![1] as UITextField
login(loginTextField.text, passwordTextField.text)
}
loginAction.enabled = false
let forgotPasswordAction = UIAlertAction(title: "Forgot Password", style: .Destructive) { (_) in }
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in }
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Login"
NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object: textField, queue: NSOperationQueue.mainQueue()) { (notification) in
loginAction.enabled = textField.text != ""
}
}
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Password"
textField.secureTextEntry = true
}
alertController.addAction(loginAction)
alertController.addAction(forgotPasswordAction)
alertController.addAction(cancelAction)
創(chuàng)建注冊(cè)表單

UIAlertController 想得比以前更周到一些,它提供了展示無(wú)限個(gè)輸入框的權(quán)限,并且每一個(gè)輸入框都可以根據(jù)需求進(jìn)行自定義。這讓僅在一個(gè)模態(tài)的 alert 中實(shí)現(xiàn)完善的注冊(cè)功能成為可能:
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Email"
textField.keyboardType = .EmailAddress
}
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Password"
textField.secureTextEntry = true
}
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Password Confirmation"
textField.secureTextEntry = true
}
雖然有了這些,但是必須要說(shuō)明的是,這只是為登錄注冊(cè)的實(shí)現(xiàn)提供一個(gè)可能,我們最好還是自定義一個(gè)ViewController來(lái)做注冊(cè)功能,因?yàn)槲覀儽緛?lái)就改這樣做。