标签:
警告显示用户向用户显示一个简洁和信息的警告信息。警告视图传达了一个应用程序或设备的重要信息,打断了用户,要求他们停止他们正在做的选择行动或解雇警报。例如,iOS使用警报,警告用户电池电量不足,所以他们可以在他们的工作被中断连接电源适配器。警报视图出现在顶部的应用程序的内容,必须手动解除由用户才可以与应用程序恢复作用。
<span style="font-size:14px;">// Default样式 新建控件方式1 func initUIAlertView1(){ let alertView1 = UIAlertView.init(title: "样式1", message: "测试", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定","按钮3") // Available in iOS 2.0 and later.Deprecated in iOS 9.0. alertView1.show() }</span>
func initUIAlertView2(){ let alertView2 = UIAlertView.init() alertView2.title = "样式2" alertView2.message = "测试" alertView2.addButtonWithTitle("取消") // 先添加 "取消" 按钮,它的index = 0 alertView2.addButtonWithTitle("确定") // index = 1 alertView2.delegate = self alertView2.show() }
上面样式1和样式2是一样的,只是两种不同的新建控件的方式,且样式1比样式2多了一个按钮.
func initUIAlertView3(){ let alertView3 = UIAlertView.init() alertView3.alertViewStyle = UIAlertViewStyle.SecureTextInput // 密文 alertView3.title = "SecureTextInput" alertView3.addButtonWithTitle("确定") alertView3.delegate = self alertView3.show() }
func initUIAlertView4(){ let alertView4 = UIAlertView.init() alertView4.alertViewStyle = UIAlertViewStyle.PlainTextInput // 非密文 alertView4.title = "PlainTextInput" alertView4.addButtonWithTitle("确定") alertView4.delegate = self alertView4.show() }
样式3和样式4,基本也是一样,知识视图中间的TextFiled一个是密文一个不是.
func initUIAlertView5(){ let alertView5 = UIAlertView.init() alertView5.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput // 登录用户名和密码输入样式 alertView5.title = "LoginAndPasswordInput" alertView5.addButtonWithTitle("确定") alertView5.delegate = self alertView5.show() }
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { print("title = \(alertView.buttonTitleAtIndex(buttonIndex)) buttonIndex = \(buttonIndex)") // 打印按钮的title和index if alertView.alertViewStyle == UIAlertViewStyle.LoginAndPasswordInput { print("\(alertView.textFieldAtIndex(0)?.text)") // 打印输入的内容 print("\(alertView.textFieldAtIndex(1)?.text)") // 打印输入的内容 } else{ print("\(alertView.textFieldAtIndex(0)?.text)") // 打印输入的内容 } }
这种是系统默认样式
<span style="font-size:14px;"> func initUIActionSheet1(){ let actionSheet1 = UIActionSheet.init(title: "UIActionSheet", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定")// Available in iOS 2.0 and later.Deprecated in iOS 8.3. actionSheet1.showInView(self.view) }</span>
func initUIActionSheet2(){ let actionSheet2 = UIActionSheet.init() actionSheet2.title = "我是title" actionSheet2.addButtonWithTitle("确定") actionSheet2.addButtonWithTitle("取消") actionSheet2.delegate = self actionSheet2.showInView(self.view) }
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { print("title = \(actionSheet.buttonTitleAtIndex(buttonIndex)) buttonIndex = \(buttonIndex)") // 打印按钮的title和index }
整个界面是做了7个按钮来触发这个两个控件的7种样式
<span style="font-size:14px;">@IBAction func btnClick(sender: UIButton) { let tag = sender.tag // 打印按钮tag和标题 print("tag = \(tag) title = \(sender.titleLabel?.text)") switch tag { case 1: self.initUIAlertView1() break case 2: self.initUIAlertView2() break case 3: self.initUIAlertView3() break case 4: self.initUIAlertView4() break case 5: self.initUIAlertView5() break default: break } } </span>
<span style="font-size:14px;">@IBAction func btnClick2(sender: UIButton) { let tag = sender.tag // 打印按钮tag和标题 print("tag = \(tag) title = \(sender.titleLabel?.text)") switch tag { case 1: self.initUIActionSheet1() break case 2: self.initUIActionSheet2() break default: break } }</span>
Swift UIAlertView/UIActionSheet
标签:
原文地址:http://blog.csdn.net/zhuming3834/article/details/51072291