论 Swift 开发入门 : 弹框(UIAlertController)

转载请声明出处:——————————————————————————————1、Summary

——————————————————————————————2、Code//// ViewController.swift// UIAlertControllerSample//// Created by jinnchang on 15/3/17.// Copyright (c) 2015年 Jinn Chang. All rights reserved.//// 1、在 iOS8 中官网提倡用 UIAlertController 替代 UIAlertView 和 UIActionSheet。// 原来是一个在 window 视图上的 view,,现在改成 controller,展现方式直接从当前的 controller 直接 present 出来。//// 2、UIAlertController 样式// ActionSheet// 上拉菜单// Alert// 弹框//// 3、UIAlertAction 样式// Default// 默认// Cancel// 取消// Destructive// 警示//// 4、无论添加顺序怎样,取消按钮永远都会出现在上拉菜单的底部,其他的按添加顺序排序。////import UIKitclass ViewController: UIViewController {var simpleAlert: UIAlertController!var loginAlert: UIAlertController!var simpleActionSheet: UIAlertController!override func viewDidLoad() {// 定义一个按钮控制最简单的 Alertlet button1 = UIButton.buttonWithType(.System) as UIButtonbutton1.frame = CGRectMake(self.view.frame.width/2 – 200, 50, 400, 50)button1.setTitle("simple alert", forState: UIControlState.Normal)button1.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)button1.tag = 1// 定义一个按钮控制带文本框的 Alertlet button2 = UIButton.buttonWithType(.System) as UIButtonbutton2.frame = CGRectMake(self.view.frame.width/2 – 200, 150, 400, 50)button2.setTitle("login alert", forState: UIControlState.Normal)button2.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)button2.tag = 2// 定义一个按钮控制上拉菜单let button3 = UIButton.buttonWithType(.System) as UIButtonbutton3.frame = CGRectMake(self.view.frame.width/2 – 200, 250, 400, 50)button3.setTitle("simple action sheet", forState: UIControlState.Normal)button3.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)button3.tag = 3// 定义 cancel、ok、save、delete、reset 的 UIAlertActionvar cancelAction = UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: nil)var okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.Default){(action: UIAlertAction!) -> Void inprintln("you choose ok")}var saveAction = UIAlertAction(title: "save", style: UIAlertActionStyle.Default){(action: UIAlertAction!) -> Void inprintln("you choose save")}var deleteAction = UIAlertAction(title: "delete", style: UIAlertActionStyle.Destructive){(action: UIAlertAction!) -> Void inprintln("you choose delete")}var resetAction = UIAlertAction(title: "reset", style: UIAlertActionStyle.Destructive){(action: UIAlertAction!) -> Void inprintln("you choose reset")}// 1、定义最简单的 AlertsimpleAlert = UIAlertController(title: "simple alert", message: "this is a simple alert", preferredStyle: UIAlertControllerStyle.Alert)simpleAlert.addAction(cancelAction)simpleAlert.addAction(resetAction)simpleAlert.addAction(okAction)// 2、定义带文本框的 AlertloginAlert = UIAlertController(title: "login alert", message: "please enter your name and password", preferredStyle: UIAlertControllerStyle.Alert)loginAlert.addTextFieldWithConfigurationHandler {(textField: UITextField!) -> Void intextField.placeholder = "name"}loginAlert.addTextFieldWithConfigurationHandler {(textField: UITextField!) -> Void intextField.placeholder = "password"textField.secureTextEntry = true}var loginAction = UIAlertAction(title: "login", style: UIAlertActionStyle.Default) {(action: UIAlertAction!) -> Void invar name = self.loginAlert.textFields!.first as UITextFieldvar password = self.loginAlert.textFields!.last as UITextFieldprintln("name : \(name.text); password : \(password.text)")}loginAlert.addAction(loginAction)// 3、定义上拉菜单simpleActionSheet = UIAlertController(title: "simple action sheet", message: "action sheet message", preferredStyle: UIAlertControllerStyle.ActionSheet)simpleActionSheet.addAction(cancelAction)simpleActionSheet.addAction(deleteAction)simpleActionSheet.addAction(saveAction)self.view.addSubview(button1)self.view.addSubview(button2)self.view.addSubview(button3)self.view.addSubview(button3)}/// 按钮响应事件func buttonAction(sender: UIButton) {let num = sender.tagswitch num {case 1:self.presentViewController(simpleAlert, animated: true, completion: nil)case 2:self.presentViewController(loginAlert, animated: true, completion: nil)case 3:self.presentViewController(simpleActionSheet, animated: true, completion: nil)default:break}}}——————————————————————————————3、ResourceGithub 上项目地址:UIAlertControllerSample

文章最后更新时间:2015年3月20日09:24:42。参考资料如下:

UIAlertController Class Reference

关于爱情简短的句子

论 Swift 开发入门 : 弹框(UIAlertController)

相关文章:

你感兴趣的文章:

标签云: