Swift基础(二十二)UIAlertView,UIActionSheet,UIAlertController
2016-07-26 13:25
375 查看
说明:使用UIAlertController时,在显示时,需要“模态”出这个空件,最好是自定义一个类,否则很可能会“模态”不出来
class RootViewController: UIViewController, UIAlertViewDelegate, UIActionSheetDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.whiteColor()
// UIAlertView
// 创建单一按钮提醒视图
let oneButtonAlert: UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定")
// 显示提醒视图
// oneButtonAlert.show()
// 创建多按钮提醒视图
let moreButtonAlert: UIAlertView = UIAlertView(title: "提醒", message: "多按钮提醒钮视图,请选择一个",delegate: nil, cancelButtonTitle: "确定", otherButtonTitles: "按钮 1", "按钮 2", "按钮 3", "按钮 4")
// 设置代理
moreButtonAlert.delegate = self
// moreButtonAlert.show()
// 还可以通过下面的方法创建提醒视图
// 初始化一个空的alert
let alert = UIAlertView()
// 设置标题
alert.title = "提醒"
// 设置代理
// alert.delegate = self
// 设置提醒信息
alert.message = "多按钮提醒钮视图,请选择一个按钮"
// 添加按钮,可以添加多个
alert.addButtonWithTitle("按钮 1")
alert.addButtonWithTitle("按钮 2")
alert.addButtonWithTitle("按钮 3")
alert.addButtonWithTitle("按钮 4")
// 显示
// alert.show()
/*
public enum UIAlertViewStyle : Int {
case Default 默认没有文本框
case SecureTextInput 一个密码输入框
case PlainTextInput 一个明文文本框
case LoginAndPasswordInput 具有账号输入框和密码输入框
}
*/
// 设置提醒视图样式
oneButtonAlert.alertViewStyle = .LoginAndPasswordInput
// 可以获取到提醒视图上的文本框
// let textField: UITextField? = oneButtonAlert.textFieldAtIndex(0)
// 设置文本框占位符
// textField?.placeholder = "请输入账号"
// UIActionSheet
// 操作表经常有用户分享功能的实现,展现多个分享按钮共用户选择
// 创建不带红色按钮的操作表代码
let actionSheet: UIActionSheet = UIActionSheet(title: "请选择分享方向", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "邮件", "短信", "微博", "微信")
// 显示到视图上
// actionSheet.showInView(self.view)
// 创建带有红色按钮的操作表
var actionSheet2: UIActionSheet = UIActionSheet(title: "将删除该条评论", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定删除")
// 显示到视图上
// actionSheet2.showInView(self.view)
// 常用属性
// 设置标题
actionSheet.title = "新标题"
// 设置样式
actionSheet.actionSheetStyle = UIActionSheetStyle.BlackTranslucent
// 添加按钮
actionSheet.addButtonWithTitle("新加按钮")
// 根据index坐标获取一个按钮的文本
let butIndex = actionSheet.buttonTitleAtIndex(2)
// 获取取消按钮的坐标
let cancelIndex = actionSheet.cancelButtonIndex
// 获取按钮个数
let butCount = actionSheet.numberOfButtons
// 苹果官方现在不提倡在iOS 8 中使用UIAlertView和UIActionSheet,取而代之的是UIAlertController。
// 在iOS 8 中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两项的功能和作用。使用对话框(alert)还是使用上拉菜单(actionsheet),取决于在创建控制器时是如何设置首选样式的
// 创建样式是Alert的UIAlertController
let alertController = UIAlertController(title: "提示", message: "确定退出登录?", preferredStyle: UIAlertControllerStyle.Alert)
// 创建取消按钮
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: { (action: UIAlertAction) -> Void in
print("取消")
})
// 创建确定按钮
let otherAction = UIAlertAction(title: "确定", style: .Default, handler: { (action: UIAlertAction) -> Void in
print("确定")
})
// 添加按钮
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
// 弹出
self.presentViewController(alertController, animated: true, completion: { () -> Void in
})
// 带文本框的UIAlertController,一般用于登录,
// 创建样式是Alert的UIAlertController
// 添加文本框
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
textField.placeholder = "登录"
}
// 添加文本框1
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
textField.placeholder = "密码"
textField.secureTextEntry = true
}
// 创建样式是ActionSheet的UIAlertController
let alertController1 = UIAlertController(title: "提示", message: "将删除该条评论?", preferredStyle: .ActionSheet)
// 创建取消按钮
let cancelAction1 = UIAlertAction(title: "取消", style: .Cancel, handler: { (action: UIAlertAction) -> Void in
print("取消")
})
// 创建确定按钮
let otherAction1 = UIAlertAction(title: "确定", style: .Default, handler: { (action: UIAlertAction) -> Void in
print("确定")
})
// 添加按钮
alertController1.addAction(cancelAction1)
alertController1.addAction(otherAction1)
// 弹出
self.presentViewController(alertController1, animated: true) {
}
}
// 根据被单击按钮的索引处理单击事件
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0:
print("按钮 1")
case 1:
print("按钮 2")
case 2:
print("按钮 3")
case 3:
print("按钮 4")
default:
print("单击确定")
}
}
// 取消按钮的事件
func alertViewCancel(alertView: UIAlertView) {
}
// 即将显示时的事件
func willPresentAlertView(alertView: UIAlertView) {
}
// 已经显示时的事件
func didPresentAlertView(alertView: UIAlertView) {
}
// 即将消失时的事件
func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
}
// 已经消失时的事件
func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex
b48f
: Int) {
}
// 编辑任何默认的字段添加的风格之后调用
func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool {
return false
}
// 根据被单击按钮的索引处理单击事件
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0:
print("单击取消")
case 1:
print("1")
case 2:
print("2")
case 3:
print("3")
default:
print("未知")
}
}
// 协议方法
// 选择取消按钮
func actionSheetCancel(actionSheet: UIActionSheet) {
}
// 即将显示时
func willPresentActionSheet(actionSheet: UIActionSheet) {
}
// 已显示时
func didPresentActionSheet(actionSheet: UIActionSheet) {
}
// 即将消失
func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
}
// 已消失
func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
}
}
class RootViewController: UIViewController, UIAlertViewDelegate, UIActionSheetDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.whiteColor()
// UIAlertView
// 创建单一按钮提醒视图
let oneButtonAlert: UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定")
// 显示提醒视图
// oneButtonAlert.show()
// 创建多按钮提醒视图
let moreButtonAlert: UIAlertView = UIAlertView(title: "提醒", message: "多按钮提醒钮视图,请选择一个",delegate: nil, cancelButtonTitle: "确定", otherButtonTitles: "按钮 1", "按钮 2", "按钮 3", "按钮 4")
// 设置代理
moreButtonAlert.delegate = self
// moreButtonAlert.show()
// 还可以通过下面的方法创建提醒视图
// 初始化一个空的alert
let alert = UIAlertView()
// 设置标题
alert.title = "提醒"
// 设置代理
// alert.delegate = self
// 设置提醒信息
alert.message = "多按钮提醒钮视图,请选择一个按钮"
// 添加按钮,可以添加多个
alert.addButtonWithTitle("按钮 1")
alert.addButtonWithTitle("按钮 2")
alert.addButtonWithTitle("按钮 3")
alert.addButtonWithTitle("按钮 4")
// 显示
// alert.show()
/*
public enum UIAlertViewStyle : Int {
case Default 默认没有文本框
case SecureTextInput 一个密码输入框
case PlainTextInput 一个明文文本框
case LoginAndPasswordInput 具有账号输入框和密码输入框
}
*/
// 设置提醒视图样式
oneButtonAlert.alertViewStyle = .LoginAndPasswordInput
// 可以获取到提醒视图上的文本框
// let textField: UITextField? = oneButtonAlert.textFieldAtIndex(0)
// 设置文本框占位符
// textField?.placeholder = "请输入账号"
// UIActionSheet
// 操作表经常有用户分享功能的实现,展现多个分享按钮共用户选择
// 创建不带红色按钮的操作表代码
let actionSheet: UIActionSheet = UIActionSheet(title: "请选择分享方向", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "邮件", "短信", "微博", "微信")
// 显示到视图上
// actionSheet.showInView(self.view)
// 创建带有红色按钮的操作表
var actionSheet2: UIActionSheet = UIActionSheet(title: "将删除该条评论", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定删除")
// 显示到视图上
// actionSheet2.showInView(self.view)
// 常用属性
// 设置标题
actionSheet.title = "新标题"
// 设置样式
actionSheet.actionSheetStyle = UIActionSheetStyle.BlackTranslucent
// 添加按钮
actionSheet.addButtonWithTitle("新加按钮")
// 根据index坐标获取一个按钮的文本
let butIndex = actionSheet.buttonTitleAtIndex(2)
// 获取取消按钮的坐标
let cancelIndex = actionSheet.cancelButtonIndex
// 获取按钮个数
let butCount = actionSheet.numberOfButtons
// 苹果官方现在不提倡在iOS 8 中使用UIAlertView和UIActionSheet,取而代之的是UIAlertController。
// 在iOS 8 中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两项的功能和作用。使用对话框(alert)还是使用上拉菜单(actionsheet),取决于在创建控制器时是如何设置首选样式的
// 创建样式是Alert的UIAlertController
let alertController = UIAlertController(title: "提示", message: "确定退出登录?", preferredStyle: UIAlertControllerStyle.Alert)
// 创建取消按钮
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: { (action: UIAlertAction) -> Void in
print("取消")
})
// 创建确定按钮
let otherAction = UIAlertAction(title: "确定", style: .Default, handler: { (action: UIAlertAction) -> Void in
print("确定")
})
// 添加按钮
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
// 弹出
self.presentViewController(alertController, animated: true, completion: { () -> Void in
})
// 带文本框的UIAlertController,一般用于登录,
// 创建样式是Alert的UIAlertController
// 添加文本框
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
textField.placeholder = "登录"
}
// 添加文本框1
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
textField.placeholder = "密码"
textField.secureTextEntry = true
}
// 创建样式是ActionSheet的UIAlertController
let alertController1 = UIAlertController(title: "提示", message: "将删除该条评论?", preferredStyle: .ActionSheet)
// 创建取消按钮
let cancelAction1 = UIAlertAction(title: "取消", style: .Cancel, handler: { (action: UIAlertAction) -> Void in
print("取消")
})
// 创建确定按钮
let otherAction1 = UIAlertAction(title: "确定", style: .Default, handler: { (action: UIAlertAction) -> Void in
print("确定")
})
// 添加按钮
alertController1.addAction(cancelAction1)
alertController1.addAction(otherAction1)
// 弹出
self.presentViewController(alertController1, animated: true) {
}
}
// 根据被单击按钮的索引处理单击事件
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0:
print("按钮 1")
case 1:
print("按钮 2")
case 2:
print("按钮 3")
case 3:
print("按钮 4")
default:
print("单击确定")
}
}
// 取消按钮的事件
func alertViewCancel(alertView: UIAlertView) {
}
// 即将显示时的事件
func willPresentAlertView(alertView: UIAlertView) {
}
// 已经显示时的事件
func didPresentAlertView(alertView: UIAlertView) {
}
// 即将消失时的事件
func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
}
// 已经消失时的事件
func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex
b48f
: Int) {
}
// 编辑任何默认的字段添加的风格之后调用
func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool {
return false
}
// 根据被单击按钮的索引处理单击事件
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0:
print("单击取消")
case 1:
print("1")
case 2:
print("2")
case 3:
print("3")
default:
print("未知")
}
}
// 协议方法
// 选择取消按钮
func actionSheetCancel(actionSheet: UIActionSheet) {
}
// 即将显示时
func willPresentActionSheet(actionSheet: UIActionSheet) {
}
// 已显示时
func didPresentActionSheet(actionSheet: UIActionSheet) {
}
// 即将消失
func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
}
// 已消失
func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 介绍 Fedora 上的 Swift
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程