UIAlertView/UIActionSheet
UIAlertView
1 //一个按钮的提醒 2 @IBAction func oneButtonAler() 3 { 4 //创建单一按钮提醒视图 5 let oneButtonAlert:UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定") 6 //显示提醒视图 7 oneButtonAlert.show() 8 9 //设置提醒视图样式 10 oneButtonAlert.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput 11 12 //来获取提醒视图上的文本框 13 var textField : UITextField? = oneButtonAlert.textFieldAtIndex(0) 14 15 //设置文本框占位符 16 textField?.placeholder = "请输入账号" 17 18 19 20 } 21 22 23 //多个按钮的提醒 24 @IBAction func moreButtonAler() 25 { 26 27 //创建多按钮提醒视图 28 let moreButtonAlert:UIAlertView = UIAlertView(title: "提醒", message: "多按钮提醒钮视图,请选择一个按钮", delegate: self, cancelButtonTitle: "确定" , otherButtonTitles: "按钮 1","按钮 2","按钮 3","按钮 4") 29 //显示图像视图 30 moreButtonAlert.show() 31 32 //设置代理 33 moreButtonAlert.delegate = self 34 35 36 // //方法二 37 // //初始化空得alert 38 // let alert = UIAlertView() 39 // //设置标题 40 // alert.title = "提醒" 41 // //设置代理 42 // alert.delegate = self 43 // //设置提醒信息 44 // alert.message = "多按钮提醒钮视图,请选择一个按钮" 45 // 46 // //添加按钮,可以添加多个 47 // alert.addButtonWithTitle("按钮 1") 48 // alert.addButtonWithTitle("按钮 2") 49 // alert.addButtonWithTitle("按钮 3") 50 // alert.addButtonWithTitle("按钮 4") 51 // 52 // //显示 53 // alert.show() 54 } 55 56 override func viewDidLoad() { 57 super.viewDidLoad() 58 59 titleLabel.text = titleString 60 61 62 // Do any additional setup after loading the view. 63 } 64 65 override func didReceiveMemoryWarning() { 66 super.didReceiveMemoryWarning() 67 // Dispose of any resources that can be recreated. 68 69 } 70 71 72 /* 73 // MARK: - Navigation 74 75 // In a storyboard-based application, you will often want to do a little preparation before navigation 76 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 77 // Get the new view controller using segue.destinationViewController. 78 // Pass the selected object to the new view controller. 79 } 80 */ 81 82 83 84 // MARK: - UIAlertViewDelegate 85 86 //根据被点击按钮的索引处理点击事件 87 func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) 88 { 89 switch buttonIndex 90 { 91 case 0: 92 println("按钮 1") 93 case 1: 94 println("按钮 2") 95 case 2: 96 println("按钮 3") 97 case 3: 98 println("按钮 4") 99 default: 100 println("点击确定") 101 } 102 } 103 104 //取消按钮的事件 105 func alertViewCancel(alertView: UIAlertView) 106 { 107 108 } 109 110 //即将显示时 111 func willPresentAlertView(alertView: UIAlertView) 112 { 113 114 } 115 116 //已经显示时的事件 117 func didPresentAlertView(alertView: UIAlertView) 118 { 119 120 } 121 122 //即将消失时的事件 123 func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) 124 { 125 126 } 127 128 //已经消失时的事件 129 func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) 130 { 131 132 } 133 134 //编辑任何默认的字段添加的风格之后调用 135 func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool 136 { 137 return false 138 }
UIActionSheet
1 @IBAction func showActionSheet1() 2 { 3 //创建不带红色按钮的操作表 4 var actionSheet:UIActionSheet = UIActionSheet(title: "请选择分享方向", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "邮件", "短信","微博","微信") 5 6 actionSheet.delegate = self 7 8 //显示到self.view视图上 9 actionSheet.showInView(self.view) 10 11 //设置标题 12 actionSheet.title = "新标题" 13 14 //设置样式 15 actionSheet.actionSheetStyle = UIActionSheetStyle.BlackTranslucent 16 17 //添加按钮 18 actionSheet.addButtonWithTitle("新加按钮") 19 20 //根据index坐标获取一个按钮的文本 21 var butIndex = actionSheet.buttonTitleAtIndex(2) 22 23 //获取取消按钮的坐标 24 var cancelIndex = actionSheet.cancelButtonIndex 25 26 //获取按钮个数 27 var butCount = actionSheet.numberOfButtons 28 29 30 } 31 32 33 @IBAction func showActionSheet2() 34 { 35 36 //创建带有红色按钮的操作表 37 var actionSheet2:UIActionSheet = UIActionSheet(title: "将删除该条评论", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定删除") 38 39 //显示到self.view视图上 40 actionSheet2.showInView(self.view) 41 } 42 43 44 override func viewDidLoad() { 45 super.viewDidLoad() 46 47 titleLabel.text = titleString 48 49 50 // Do any additional setup after loading the view. 51 } 52 53 override func didReceiveMemoryWarning() { 54 super.didReceiveMemoryWarning() 55 // Dispose of any resources that can be recreated. 56 } 57 58 59 /* 60 // MARK: - Navigation 61 62 // In a storyboard-based application, you will often want to do a little preparation before navigation 63 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 64 // Get the new view controller using segue.destinationViewController. 65 // Pass the selected object to the new view controller. 66 } 67 */ 68 69 // MARK: - UIActionSheetDelegate 70 71 //根据被点击按钮的索引处理点击事件 72 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) 73 { 74 switch buttonIndex 75 { 76 case 0: 77 println("点击取消") 78 case 1: 79 println("1") 80 case 2: 81 println("2") 82 case 3: 83 println("3") 84 default: 85 println("未知") 86 } 87 } 88 89 //选择取消按钮 90 func actionSheetCancel(actionSheet: UIActionSheet) 91 { 92 93 } 94 95 //即将显示时 96 func willPresentActionSheet(actionSheet: UIActionSheet) 97 { 98 99 } 100 101 //已显示时 102 func didPresentActionSheet(actionSheet: UIActionSheet) 103 { 104 105 } 106 107 //即将消失 108 func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) 109 { 110 111 } 112 113 //已消失 114 func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) 115 { 116 117 }
时间: 2024-10-10 09:27:52