前言

	NS_CLASS_DEPRECATED_IOS(2_0, 8_3, "UIActionSheet is deprecated. Use UIAlertController with a
preferredStyle of UIAlertControllerStyleActionSheet instead") __TVOS_PROHIBITED
@interface UIActionSheet : UIView @available(iOS, introduced=2.0, deprecated=8.3, message="UIActionSheet is deprecated. Use
UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead")
public class UIActionSheet : UIView
  • 按钮的 index 按照 otherButton、cancelButton、addButtonWith 的顺序依次类推,起始值为 0。

  • ActionSheet 也可以设置 title 属性作为提示信息,一般不设置 title 看着会舒服一些。

  • ActionSheet 显示的时候调用的是 showInView。如果是在工具条或标签条中需要使用专门提供的其它方法。

    • 工具条的情况下 [actionSheet showFromToolbar:self.navigationController.toolbar];
    • 标签条的情况下 [actionSheet showFromTabBar:self.tabBar];

1、UIActionSheet 的创建

  • Objective-C

    • 创建时直接添加按钮等信息

      	// 设置代理时,需遵守协议 <UIActionSheetDelegate>
      UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择"
      delegate:self
      cancelButtonTitle:@"取消"
      destructiveButtonTitle:@"destructive"
      otherButtonTitles:@"动作 1", @"动作 2", @"动作 3", nil]; // 将 actionSheet 添加到 view
      [actionSheet showInView:self.view];
    • 先创建,后添加按钮等信息

      	// 设置代理时,需遵守协议 <UIActionSheetDelegate>
      UIActionSheet *actionSheet = [[UIActionSheet alloc] init]; actionSheet.title = @"请选择";
      [actionSheet addButtonWithTitle:@"取消"];
      [actionSheet addButtonWithTitle:@"动作 1"];
      [actionSheet addButtonWithTitle:@"动作 2"];
      [actionSheet addButtonWithTitle:@"动作 3"];
      actionSheet.cancelButtonIndex = 0;
      actionSheet.delegate = self; // 将 actionSheet 添加到 view
      [actionSheet showInView:self.view];
  • Swift

    • 创建时直接添加按钮等信息

      	// 设置代理时,需遵守协议 UIActionSheetDelegate
      let actionSheet:UIActionSheet = UIActionSheet(title: "请选择",
      delegate: self,
      cancelButtonTitle: "取消",
      destructiveButtonTitle: "destructive",
      otherButtonTitles: "动作 1", "动作 2", "动作 3") // 将 actionSheet 添加到 view
      actionSheet.showInView(self.view)
    • 先创建,后添加按钮等信息

      	// 设置代理时,需遵守协议 UIActionSheetDelegate
      let actionSheet:UIActionSheet = UIActionSheet() actionSheet.title = "请选择"
      actionSheet.addButtonWithTitle("取消")
      actionSheet.addButtonWithTitle("动作 1")
      actionSheet.addButtonWithTitle("动作 2")
      actionSheet.addButtonWithTitle("动作 3")
      actionSheet.cancelButtonIndex = 0
      actionSheet.delegate = self // 将 actionSheet 添加到 view
      actionSheet.showInView(self.view)

2、UIActionSheet 的设置

  • Objective-C

    	// 设置样式
    /*
    // take appearance from toolbar style otherwise uses 'default'
    UIActionSheetStyleAutomatic = -1, UIActionSheetStyleDefault = UIBarStyleDefault,
    UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
    UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque ,
    */
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault; // 设置标题
    actionSheet.title = @"系统提示"; // 添加按钮
    /*
    需要放在 [actionSheet showInView:self]; 前才起作用
    */
    [actionSheet addButtonWithTitle:@"确定"]; // 设置最下边位置的按钮
    /*
    设置最下边位置的按钮: 大于 0 并且 小于等于按钮的总个数,按照 otherButton、cancelButton、addButtonWith 的顺序依次显示
    在最下边位置。
    等于 0 或者 大于按钮的总个数,按照 otherButton、cancelButton、addButtonWith 的顺序依次显示在最
    下边位置,并且取消最下边一个按钮和上边按钮的间隔。
    */
    actionSheet.cancelButtonIndex = 4; // 获取指定位置按钮的标题
    NSString *buttonTitle = [actionSheet buttonTitleAtIndex:5]; // 获取按钮的个数,只读
    NSInteger numberOfButtons = actionSheet.numberOfButtons; // 获取除取消按钮外第一个按钮的索引,只读
    NSInteger firstOtherButtonIndex = actionSheet.firstOtherButtonIndex; // 获取 actionSheet 是否已经显示出来,只读
    BOOL alertViewVisible = actionSheet.isVisible; // 显示 actionSheet
    [actionSheet showInView:self.view]; // 隐藏 actionSheet
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; // 设置代理,需遵守协议 <UIActionSheetDelegate>
    actionSheet.delegate = self;
  • Swift

    	// 设置样式
    /*
    case Automatic // take appearance from toolbar style otherwise uses 'default'
    case Default
    case BlackTranslucent
    case BlackOpaque
    */
    actionSheet.actionSheetStyle = .Default // 设置标题
    actionSheet.title = "系统提示" // 添加按钮
    /*
    需要放在 [actionSheet showInView:self]; 前才起作用
    */
    actionSheet.addButtonWithTitle("确定") // 设置最下边位置的按钮
    /*
    设置最下边位置的按钮: 大于 0 并且 小于等于按钮的总个数,按照 otherButton、cancelButton、addButtonWith 的顺序依次显示
    在最下边位置。
    等于 0 或者 大于按钮的总个数,按照 otherButton、cancelButton、addButtonWith 的顺序依次显示在最
    下边位置,并且取消最下边一个按钮和上边按钮的间隔。
    */
    actionSheet.cancelButtonIndex = 4 // 获取指定位置按钮的标题
    let buttonTitle:String? = actionSheet.buttonTitleAtIndex(5) // 获取按钮的个数,只读
    let numberOfButtons:NSInteger = actionSheet.numberOfButtons // 获取除取消按钮外第一个按钮的索引,只读
    let firstOtherButtonIndex:NSInteger = actionSheet.firstOtherButtonIndex // 获取 actionSheet 是否已经显示出来,只读
    let alertViewVisible:Bool = actionSheet.visible // 显示 actionSheet
    actionSheet.showInView(self.view) // 隐藏 actionSheet
    actionSheet.dismissWithClickedButtonIndex(0, animated: true) // 设置代理,需遵守协议 UIActionSheetDelegate
    actionSheet.delegate = self

3、UIActionSheetDelegate 的协议方法

  • 需遵守协议 UIActionSheetDelegate,并设置代理

  • Objective-C

    	// 将要显示,操作表显示前被调用
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet { } // 已经显示,操作表显示后被调用
    - (void)didPresentActionSheet:(UIActionSheet *)actionSheet { } // 将要结束选择那个按钮,操作表关闭前被调用
    - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { } // 已经结束了选择那个按钮,操作表关闭后被调用,操作表显示中应用程序进入睡眠状态时也会被调用
    -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { } // 点击了那个按钮,触摸操作表中的任意按钮时被调用,比 willDismissWithButtonIndex 先被调用
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { // 按钮的 index 按照 otherButton、cancelButton、addButtonWith 的顺序依次类推
    } // 强制关闭,操作表显示中强制关闭时被调用,例如操作表显示时应用程序突然关闭等场所
    -(void)actionSheetCancel:(UIActionSheet *)actionSheet { }
  • Swift

    	// 将要显示,操作表显示前被调用
    func willPresentActionSheet(actionSheet: UIActionSheet) { } // 已经显示,操作表显示后被调用
    func didPresentActionSheet(actionSheet: UIActionSheet) { } // 将要结束选择那个按钮,操作表关闭前被调用
    func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) { } // 已经结束了选择那个按钮,操作表关闭后被调用,操作表显示中应用程序进入睡眠状态时也会被调用
    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) { } // 点击了那个按钮,触摸操作表中的任意按钮时被调用,比 willDismissWithButtonIndex 先被调用
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { // 按钮的 index 按照 otherButton、cancelButton、addButtonWith 的顺序依次类推
    } // 强制关闭,操作表显示中强制关闭时被调用,例如操作表显示时应用程序突然关闭等场所
    func actionSheetCancel(actionSheet: UIActionSheet) { }

iOS - UIActionSheet的更多相关文章

  1. IOS UIActionSheet的使用方法

    在IOS的用户接口向导中,苹果提供了另外一种显示警告框的手法,叫做UIActionSheet.它和UIAlertView比起来不会显得过于急切和紧张.而是很温和地在继续流程之前给用户提供了诸多选择. ...

  2. IOS UIActionSheet(底部 弹出框的使用)

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancel ...

  3. Android开源项目库汇总

    最近做了一个Android开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. 抽 ...

  4. GitHub上受欢迎的Android UI Library

    GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...

  5. Android UI相关开源项目库汇总

    最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ...

  6. GitHub 上受欢迎的 Android UI Library 整理二

    通知 https://github.com/Tapadoo/Alerter ★2528 - 克服Toast和Snackbar的限制https://github.com/wenmingvs/Notify ...

  7. 最新最全的 Android 开源项目合集

    原文链接:https://github.com/opendigg/awesome-github-android-ui 在 Github 上做了一个很新的 Android 开发相关开源项目汇总,涉及到 ...

  8. wesome-android

    awesome-android Introduction android libs from github System requirements Android Notice If the lib ...

  9. GitHub 上受欢迎的 Android UI Library整理

    https://github.com/Tapadoo/Alerter ★2528 - 克服Toast和Snackbar的限制 https://github.com/wenmingvs/NotifyUt ...

随机推荐

  1. 6.1:SportStore:一个真实的应用

    之前的小例子让我们演示了AngularJS的一些特性,但他们缺少上下文.要解决这个问题,作者要创建一个简单单真实的电子商务应用. 作者将创建一个在线产品分类,客户可以通过分类和页面浏览,一个购物车用户 ...

  2. office 2016 专业增强版 和 visio 2016 专业版 下载安装(附带激活工

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://babyshen.blog.51cto.com/8405584/1697910 o ...

  3. React-Native入门指导之iOS篇

    React-Native 入门指导系列教程目录 一.准备工作 (已完成) 二.项目介绍与调试 三.CSS样式与Flex布局 四.常用UI控件的使用 五.JSX在React-Native中的应用 六.事 ...

  4. PHP serialize & JSON 解析

    对于JSON(JavaScript Object Notation)大家应该不陌生,它是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Program ...

  5. 对页面制定区域进行打印,以及打印不显示页脚URL的方法

    第一种方式 - 此种方式简单易用,但不能进行页面设置,会在底部显示页面的URL地址. 打印命令:只需在页面上的按钮事件调用这段JS代码 javascript:window.print(); ===== ...

  6. 如何修改ECShop发货单查询显示个数

    使用ecshop的朋友都知道,商城首页调用的发货单查询,默认显示的10个.很多朋友想修改它的数量,可是在后台管理却找不到相应的地方,这个修改和显示排行榜的数量修改方法不一样.排行榜是可以在后台修改的, ...

  7. mini.open打开窗口时传递参数

    mini.open({ url: "xxx.html", showMaxButton: false, allowResize: false, title: '标题', width: ...

  8. 1029 C语言文法定义与C程序的推导过程

    1 阅读并理解提供给大家的C语言文法文件. 2 参考该文件写出一个自己好理解版的现实版的完整版的C语言文法. 3 给出一段C程序,写出用上述文法产生这段C程序的推导过程. program → exte ...

  9. Sqlite查询时间段内的数据问题解决!

    最近搞Sqlite本地查询,需求为查询某时间段内的数据,在SQL中我们都知道为: select * from tblName where rDate Between '2008-6-10' and   ...

  10. MySQL常用操作总结

    MySQL常用操作 前提条件:已安装MySQL. 学习目标:用一条sql语句写出A和B的剩余数量 AA表 BB表 以上为一道面试题,接下来由这道面试题来回顾一些数据库的基本操作. 登录MySQL su ...