Swift开发:弹框(UIAlertController)

来源:互联网 发布:端口打开失败怎么回事 编辑:程序博客网 时间:2024/04/29 11:10

概述

黄色圆圈代表属性;绿色勾号代表方法。

------------------------------------------------------------------------------------------

代码示例

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import UIKit  
  2.   
  3. class ViewController: UIViewController {  
  4.       
  5.     var alert1: UIAlertController!  
  6.     var alert2: UIAlertController!  
  7.     var actionSheet: UIAlertController!  
  8.       
  9.     override func viewDidLoad() {  
  10.   
  11.         // 定义一个按钮,显示最简单的 Alert  
  12.         let button1 = UIButton.buttonWithType(.System) as UIButton  
  13.         button1.frame = CGRectMake(self.view.frame.width/2 - 2005040050)  
  14.         button1.setTitle("最简单的 Alert", forState: UIControlState.Normal)  
  15.         button1.addTarget(self, action"buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)  
  16.         button1.tag = 1  
  17.           
  18.         // 定义一个按钮,显示带文本框的 Alert  
  19.         let button2 = UIButton.buttonWithType(.System) as UIButton  
  20.         button2.frame = CGRectMake(self.view.frame.width/2 - 20015040050)  
  21.         button2.setTitle("带文本框的 Alert", forState: UIControlState.Normal)  
  22.         button2.addTarget(self, action"buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)  
  23.         button2.tag = 2  
  24.           
  25.         // 定义一个按钮,显示上拉菜单  
  26.         let button3 = UIButton.buttonWithType(.System) as UIButton  
  27.         button3.frame = CGRectMake(self.view.frame.width/2 - 20025040050)  
  28.         button3.setTitle("上拉菜单", forState: UIControlState.Normal)  
  29.         button3.addTarget(self, action"buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)  
  30.         button3.tag = 3  
  31.           
  32.         // 定义 cancel、ok、save、delete、reset 的 UIAlertAction  
  33.         var cancelAction = UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: nil)  
  34.         var okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.Default){  
  35.             (action: UIAlertAction!) -> Void in  
  36.             println("you choose ok")  
  37.         }  
  38.         var saveAction = UIAlertAction(title: "save", style: UIAlertActionStyle.Default){  
  39.             (action: UIAlertAction!) -> Void in  
  40.             println("you choose save")  
  41.         }  
  42.         var deleteAction = UIAlertAction(title: "delete", style: UIAlertActionStyle.Destructive){  
  43.             (action: UIAlertAction!) -> Void in  
  44.             println("you choose delete")  
  45.         }  
  46.         var resetAction = UIAlertAction(title: "reset", style: UIAlertActionStyle.Destructive){  
  47.             (action: UIAlertAction!) -> Void in  
  48.             println("you choose reset")  
  49.         }  
  50.           
  51.         // 1、初始化最简单的 Alert  
  52.         alert1 = UIAlertController(title: "simple alert", message"this is a simple alert", preferredStyle: UIAlertControllerStyle.Alert)  
  53.         alert1.addAction(cancelAction)  
  54.         alert1.addAction(resetAction)  
  55.         alert1.addAction(okAction)  
  56.           
  57.         // 2、初始化带文本框的 Alert  
  58.         alert2 = UIAlertController(title: "login alert", message"please enter your name and password", preferredStyle: UIAlertControllerStyle.Alert)  
  59.         alert2.addTextFieldWithConfigurationHandler {  
  60.             (textField: UITextField!) -> Void in  
  61.             textField.placeholder = "name"  
  62.         }  
  63.         alert2.addTextFieldWithConfigurationHandler {  
  64.             (textField: UITextField!) -> Void in  
  65.             textField.placeholder = "password"  
  66.             textField.secureTextEntry = true  
  67.         }  
  68.         var loginAction = UIAlertAction(title: "login", style: UIAlertActionStyle.Default) {  
  69.             (action: UIAlertAction!) -> Void in  
  70.             var name = self.alert2.textFields!.first as UITextField  
  71.             var password = self.alert2.textFields!.last as UITextField  
  72.             println("name : \(name.text); password : \(password.text)")  
  73.         }  
  74.         alert2.addAction(loginAction)  
  75.           
  76.         // 3、初始化上拉菜单  
  77.         actionSheet = UIAlertController(title: "simple action sheet", message"action sheet message", preferredStyle: UIAlertControllerStyle.ActionSheet)  
  78.         actionSheet.addAction(cancelAction)  
  79.         actionSheet.addAction(deleteAction)  
  80.         actionSheet.addAction(saveAction)  
  81.           
  82.         self.view.addSubview(button1)  
  83.         self.view.addSubview(button2)  
  84.         self.view.addSubview(button3)  
  85.         self.view.addSubview(button3)  
  86.     }  
  87.       
  88.     /// 按钮响应事件  
  89.     func buttonAction(sender: UIButton) {  
  90.         let num = sender.tag  
  91.         switch num {  
  92.         case 1:  
  93.             self.presentViewController(alert1, animatedtrue, completion: nil)  
  94.         case 2:  
  95.             self.presentViewController(alert2, animatedtrue, completion: nil)  
  96.         case 3:  
  97.             self.presentViewController(actionSheet, animatedtrue, completion: nil)  
  98.         default:  
  99.             break  
  100.         }  
  101.     }  
  102.       
  103. }  

------------------------------------------------------------------------------------------
结果展示

//官方地址

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/doc/uid/TP40014538-CH1-SW2

0 0
原创粉丝点击