UIKit框架-高级控件Swift版本: 7.UIActionSheet方法/属性详解
来源:互联网 发布:小嶋阳菜毕业知乎 编辑:程序博客网 时间:2024/06/01 09:49
前面我们学习了 iOS 中得第一个提示性控件 UIAlertView, 现在我们把第二个提示性控件也学完.
1.UIActionSheet常用属性
// 1.设置 UIActionSheet 的代理对象var delegate: UIActionSheetDelegate?// 2.设置 UIActionSheet 的标题var title: String// 3.设置 UIActionSheet 的样式var actionSheetStyle: UIActionSheetStyle// 4.读取 UIActionSheet 里有多少个按钮var numberOfButtons: Int { get }// 5.设置 UIActionSheet 取消按钮的索引var cancelButtonIndex: Int// 6.设置其他按钮的索引var destructiveButtonIndex: Int// 7.读取 UIActionSheet 其他按钮的索引值var firstOtherButtonIndex: Int { get }// 8.读取 UIActionSheet 是否可见var visible: Bool { get }
2.UIActionSheet的常用方法
// 1.该方法是用来添加 UIActionSheet 的按钮标题func addButtonWithTitle(title: String) -> Int// 2.该方法是用来设置 UIActionSheet 的按钮索引func buttonTitleAtIndex(buttonIndex: Int) -> String// 3.该方法是来用设置 UIActionSheet 显示到 ToolBarfunc showFromToolbar(view: UIToolbar!)// 4.该方法是用来设置 UIActionSheet 显示到 TabBarfunc showFromTabBar(view: UITabBar!)// 5.该方法是用来设置来自 UIBarButtonItem 的 UIActionSheet, 并且是否开启动画效果func showFromBarButtonItem(item: UIBarButtonItem!, animated: Bool)// 6.该方法是用来设置 UIActionSheet 的显示的视图大小, 以及指定视图和是否开启动画效果func showFromRect(rect: CGRect, inView view: UIView!, animated: Bool)// 7.该方法是用来设置 UIActionSheet 显示到哪一个视图func showInView(view: UIView!)// 8.该方法是用来设置 UIActionSheet 消失的按钮索引, 以及是否使用动画func dismissWithClickedButtonIndex(buttonIndex: Int, animated: Bool)
3.UIActionSheet的代理方法
// 1.该方法是在 UIActionSheet 上的按钮被点击时调用的 optional func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)// 2.该方法是在 UIActionSheet 上的点击了取消按钮时调用的 optional func actionSheetCancel(actionSheet: UIActionSheet)// 3.该方法是在 UIActionSheet 完全即将显示的时候调用的 optional func willPresentActionSheet(actionSheet: UIActionSheet)// 4.该方法是在 UIActionSheet 完全显示的时候调用的 optional func didPresentActionSheet(actionSheet: UIActionSheet)// 5.该方法是在 UIActionSheet 完全即将消失的时候调用的 optional func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int)// 6.该方法是在 UIActionSheet 完全消失的时候调用的 optional func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int)
3.代码演示
首先遵守代理协议
class ViewController: UIViewController, UIActionSheetDelegate {}
自定义UIButton并且监听 ActionSheet 方法
func myButton() { var button: UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton button.frame = CGRectMake(100, 200, 50, 20) button.setTitle("弹窗", forState: UIControlState.Normal) button.backgroundColor = UIColor.redColor() button.addTarget(self, action: "myActionSheet", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) }
自定义UIActionSheet
func myActionSheet() { // 1.自定义 UIActionSheet, 并且设置标题, 代理对象, 以及按钮的标题 var actionSheet = UIActionSheet(title: "UIActionSheet", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "按钮一") // 2.设置 UIActionSheet 的样式 actionSheet.actionSheetStyle = UIActionSheetStyle.Default // 3.设置取消按钮的索引 actionSheet.cancelButtonIndex = 1 // 4.设置destructive的索引值 actionSheet.destructiveButtonIndex = 0 // 5.添加其他按钮的标题 actionSheet.addButtonWithTitle("按钮二") // 6.设置按钮标题的索引 actionSheet.buttonTitleAtIndex(1) // 7.显示到 self.view 上 actionSheet.showInView(self.view) }
实现UIActionSheet代理方法
// 1.该方法是在 UIActionSheet 上的按钮被点击时调用的 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { println("被点击了") } // 2.该方法是在 UIActionSheet 上的点击了取消按钮时调用的 func actionSheetCancel(actionSheet: UIActionSheet) { println("点击了取消按钮") } // 3.该方法是在 UIActionSheet 完全即将显示的时候调用的 func willPresentActionSheet(actionSheet: UIActionSheet) { println("UIActionSheet即将显示") } // 4.该方法是在 UIActionSheet 完全显示的时候调用的 func didPresentActionSheet(actionSheet: UIActionSheet) { println("UIActionSheet完全显示") } // 5.该方法是在 UIActionSheet 完全即将消失的时候调用的 func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) { println("UIActionSheet即将消失") } // 6.该方法是在 UIActionSheet 完全消失的时候调用的 func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) { println("UIActionSheet完全消失") }
在viewDidLoad实现
override func viewDidLoad() { super.viewDidLoad() self.myButton() }
4.最终效果
PS: UIActionSheet 是继承于UIView 的,所以 UIView 里的方法和属性 UIActionSheet 都是可以使用的.
好了, 这次我们就讲到这里, 下次我们继续
0 0
- UIKit框架-高级控件Swift版本: 7.UIActionSheet方法/属性详解
- UIKit框架-高级控件Swift版本: 1.UIScrollView方法/属性详解
- UIKit框架-高级控件Swift版本: 4.UICollectionView方法/属性详解
- UIKit框架-高级控件Swift版本: 2.UITableView方法/属性详解
- UIKit框架-高级控件Swift版本: 3.UITableViewCell方法/属性详解
- UIKit框架-高级控件Swift版本: 5.UITextView方法/属性详解
- UIKit框架-高级控件Swift版本: 6.UIAlertView方法/属性详解
- UIKit框架-高级控件Swift版本: 8.UISegemtedControl方法/属性详解
- UIKit框架-高级控件Swift版本: 9.UINavigationController方法/属性详解
- UIKit框架-高级控件Swift版本: 10.UIWebView方法/属性详解
- UIKit框架-高级控件Swift版本: 9.UINavigationController方法/属性详解
- UIKit框架-基础控件Swift版本: 7.UISwitch方法/属性详解
- UIKit框架-基础控件Swift版本: 1.UIControl方法/属性详解
- UIKit框架-基础控件Swift版本: 2.UIButton方法/属性详解
- UIKit框架-基础控件Swift版本: 3.UILabel方法/属性详解
- UIKit框架-基础控件Swift版本: 4.UIImageView方法/属性详解
- UIKit框架-基础控件Swift版本: 5.UIImage方法/属性详解
- UIKit框架-基础控件Swift版本: 6.UITextField方法/属性详解
- Project Euler Problem 30
- parse_str 与 http_build_query
- c基础+数据结构+线程+冒泡实现--航班查询系统
- 49:Anagrams【哈希】【字符串】
- Linux内核如何装载和启动一个可执行程序
- UIKit框架-高级控件Swift版本: 7.UIActionSheet方法/属性详解
- windows command shell download file
- Arduino 语法手册
- Get,Post请求中文乱码问题有效解决方法
- C做题笔记(一)
- Unity人物三连击Animator实现
- 浅谈指针
- Project Euler Problem 32
- java集合:Collection和Map的介绍