弹出对话框 UIAlertController
双选
//实例化UIAlertController var av=UIAlertController(title: "💗终极大考验💗",message: "你爱妞妞妈?",preferredStyle: UIAlertControllerStyle.Alert) //实例化第一个选项 var act1 = UIAlertAction(title: "爱", style: UIAlertActionStyle.Default){ (action: UIAlertAction!) -> Void in self.lbl1.text="you choosed 爱" //回调设置lbl1的文本 } //实例化第二个选项 var act2 = UIAlertAction(title: "超爱", style: UIAlertActionStyle.Destructive){ (action: UIAlertAction!) -> Void in self.lbl1.text="you choosed 超爱" } //将选项加入对话框 av.addAction(act1) av.addAction(act2) //显示对话框 self.presentViewController(av, animated: true, completion: nil)
文本框输入
var alertController=UIAlertController(title: "请登录", message: "输入登录信息", preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in textField.placeholder = "登录" } alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in textField.placeholder = "密码" textField.secureTextEntry = true } var okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in var login = alertController.textFields?.first as UITextField var password = alertController.textFields?.last as UITextField self.lbl1.text=login.text+password.text } var cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Default,handler:nil) alertController.addAction(okAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion: nil)
带验证的文本框输入
var alertController=UIAlertController(title: "请登录", message: "输入登录信息", preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in //添加用户名输入框 textField.placeholder = "用户名" } alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in //添加密码输入框 textField.placeholder = "密码" //不显示明文 textField.secureTextEntry = true //添加一个Observer在更改内容是调用alertTextFieldDidChange函数 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("alertTextFieldDidChange:"), name: UITextFieldTextDidChangeNotification, object: textField) } alertController.addTextFieldWithConfigurationHandler{ (textField: UITextField!) -> Void in //为了显示消息 添加一个文本框 textField.enabled=false //设置标志 textField.tag=1 } //创建一个确定按钮 var okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in //获取所有文本框 var tfs=(alertController.textFields?)! //获取用户名文本框 var login = tfs[0] as UITextField //获取密码文本框 var password = tfs[1] as UITextField //设置主界面的lbl1文字 self.lbl1.text=login.text+password.text //移除已添加的Observer NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil) } var cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Default){ (action:UIAlertAction!) -> Void in //移除已添加的Observer NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil) } //添加按钮 alertController.addAction(cancelAction) alertController.addAction(okAction) okAction.enabled = false self.presentViewController(alertController, animated: true, completion: nil)
func alertTextFieldDidChange(notification: NSNotification){ //获取当前的alertController var alertController = self.presentedViewController as UIAlertController? if (alertController != nil) { //alertController里所有的文本框 var tfs=(alertController!.textFields?)! //通过索引获取密码文本框 var passwordTf = tfs[1] as UITextField //获取确定按钮 var okAction = alertController!.actions.last as UIAlertAction //取得所有文本框的数量 var count=tfs.count //循环所有文本框 for i in 0..<count { var tf=tfs[i] as UITextField //判断标志 if(tf.tag==1){ //根据密码长度设置消息文本框文本 tf.text=(countElements(passwordTf.text) > 2) ? "" : "密码长度必须大于2个字符" } } //设定确定按键是否可用 okAction.enabled = countElements(passwordTf.text) > 2 } }
转载必须保留以下信息
此文章出自博客园中SadlyCodes的博客 http://www.cnblogs.com/SadlyCodes
posted on 2015-01-18 11:34 SadlyCodes 阅读(483) 评论(0) 收藏 举报
浙公网安备 33010602011771号