Swift – UIAlertController

iOS 8 之后苹果出了一个UIAlertController,这个使用起来会比原来的UIAlertView 更加灵活一些,同时也可以集成上拉菜单。可以任意添加textfield,并且通过block来更方便的实现内部方法。
下面写一下简单集成,首先创建一个UIAlertController:
[code lang=”Objective-C”]
let alertController = UIAlertController(title: “标题”, message: “这个是UIAlertController的默认样式”, preferredStyle: UIAlertControllerStyle.Alert)
// 添加两个TextField
alertController.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
textField.placeholder = “登录”
// 在此可以添加一个textField的监听,对里面输入的内容可以进行分辨
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“alertTextFieldDidChange:”), name: UITextFieldTextDidChangeNotification, object: textField)
}
alertController.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
textField.placeholder = “密码”
textField.secureTextEntry = true
}
// 取消按钮
let cancelAction = UIAlertAction(title: “取消”, style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(cancelAction)
// 普通按钮(在此可以直接添加按钮点击的响应事件)
let okAction = UIAlertAction(title: “好的”, style: UIAlertActionStyle.Default) {
(action: UIAlertAction!) -> Void in
let login = (alertController.textFields?.first)! as UITextField
let password = (alertController.textFields?.last)! as UITextField
print(“\(login.text)—\(password.text)”)
// 上一个添加的textField监听可以在此移除
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil)
}

alertController.addAction(okAction)
// 警告按钮
let resetAction = UIAlertAction(title: “警告”, style: UIAlertActionStyle.Destructive, handler: nil)
alertController.addAction(resetAction)

presentViewController(alertController, animated: true, completion: nil)
然后需要实现textField的监听方法

func alertTextFieldDidChange(notification: NSNotification){
let alertController = presentedViewController as! UIAlertController?
if (alertController != nil) {
let login = (alertController!.textFields?.first)! as UITextField
let okAction = alertController!.actions.last! as UIAlertAction
// 读取字符串长度(1汉字=3长度)
print("\(login.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))")
// 读取字符串长度(1汉字=1长度)
print("\(login.text?.characters.count)")
}
}
[/code]
接下来写下上拉菜单的集成:
[code lang="Objective-C"]
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .ActionSheet)

let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)
let archiveAction = UIAlertAction(title: "保存", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(cancelAction)
alert.addAction(deleteAction)
alert.addAction(archiveAction)

self.presentViewController(alert, animated: true, completion: nil)

// 此代码为iPad上运行时所需要,在iPad上不需要写取消按钮,点击其他区域默认取消
let popover = alert.popoverPresentationController
if (popover != nil){
popover?.sourceView = sender
popover?.sourceRect = sender.bounds
popover?.permittedArrowDirections = UIPopoverArrowDirection.Any
}[/code]