iOS - UIActionSheet

简介: 前言 NS_CLASS_DEPRECATED_IOS(2_0, 8_3, "UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleA...

前言

    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开发 索引
IOS中UIActionSheet使用详解
IOS中UIActionSheet使用详解
269 0
|
Android开发 iOS开发
iOS 8 引入的 UIActionSheet 和 UIAlertView 的替代品 - UIAlertController
iOS 8 引入的 UIActionSheet 的替代品 - UIAlertController 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1116 0
|
Android开发 iOS开发 HTML5
iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?
iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1191 0
|
iOS开发 C++
iOS学习之UIActionSheet的使用
UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。 为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。
808 0
|
9天前
|
安全 Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的深度对比
在移动应用开发的广阔天地中,安卓和iOS两大平台各占半壁江山。本文旨在通过数据驱动的分析方法,深入探讨这两大操作系统在开发环境、用户界面设计及市场表现等方面的差异。引用最新的行业报告和科研数据,结合技术专家的观点,本文将提供对开发者和市场分析师均有价值的洞见。
|
12天前
|
Java 开发工具 Android开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的广阔天地中,Android和iOS两大平台各自占据着半壁江山。本文将深入探讨这两个平台在开发过程中的关键差异点,包括编程语言、开发工具、用户界面设计、性能优化以及市场覆盖等方面。通过对这些关键因素的比较分析,旨在为开发者提供一个清晰的指南,帮助他们根据项目需求和目标受众做出明智的平台选择。
|
2天前
|
前端开发 编译器 iOS开发
探索iOS开发的未来:SwiftUI和Combine的革新之路
随着苹果不断推动其操作系统的进化,iOS开发领域正经历着一场前所未有的变革。本文将深入分析SwiftUI和Combine框架如何重塑iOS应用的开发流程,通过对比传统MVC模式与现代SwiftUI的差异,揭示Combine响应式编程范式在简化异步代码方面的巨大潜力。文章还将探讨这些技术如何影响开发者的生产力、应用的性能以及最终用户的体验。通过案例分析和数据支持,我们将展望iOS开发的新趋势,并讨论如何在不断变化的技术环境中保持竞争力。