加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

UIKit框架-高级控件Swift版本: 7.UIActionSheet方法/属性详解

发布时间:2020-12-14 02:14:38 所属栏目:百科 来源:网络整理
导读:前面我们学习了 iOS 中得第一个提示性控件 UIAlertView,现在我们把第二个提示性控件也学完. 1.UIActionSheet常用属性 // 1.设置 UIActionSheet 的代理对象 var delegate : UIActionSheetDelegate? // 2.设置 UIActionSheet 的标题 var title: String // 3.设

前面我们学习了 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 显示到 ToolBar
func showFromToolbar(view: UIToolbar!)

// 4.该方法是用来设置 UIActionSheet 显示到 TabBar
func 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 都是可以使用的.


好了,这次我们就讲到这里,下次我们继续

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读