iOS - UIActionSheet
前言
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的更多相关文章
- IOS UIActionSheet的使用方法
在IOS的用户接口向导中,苹果提供了另外一种显示警告框的手法,叫做UIActionSheet.它和UIAlertView比起来不会显得过于急切和紧张.而是很温和地在继续流程之前给用户提供了诸多选择. ...
- IOS UIActionSheet(底部 弹出框的使用)
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancel ...
- Android开源项目库汇总
最近做了一个Android开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. 抽 ...
- GitHub上受欢迎的Android UI Library
GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...
- Android UI相关开源项目库汇总
最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ...
- GitHub 上受欢迎的 Android UI Library 整理二
通知 https://github.com/Tapadoo/Alerter ★2528 - 克服Toast和Snackbar的限制https://github.com/wenmingvs/Notify ...
- 最新最全的 Android 开源项目合集
原文链接:https://github.com/opendigg/awesome-github-android-ui 在 Github 上做了一个很新的 Android 开发相关开源项目汇总,涉及到 ...
- wesome-android
awesome-android Introduction android libs from github System requirements Android Notice If the lib ...
- GitHub 上受欢迎的 Android UI Library整理
https://github.com/Tapadoo/Alerter ★2528 - 克服Toast和Snackbar的限制 https://github.com/wenmingvs/NotifyUt ...
随机推荐
- OperService.class.php
<?php class OperSerVice{ public function getResult($num1,num2,oper){ switch($oper){ case "+& ...
- 20145227《Java程序设计》第10周学习总结
20145227<Java程序设计>第10周学习总结 教材学习内容总结 网络编程 就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收 ...
- UEditor上传功能
参考文件http://www.itnose.net/detail/6307204.html 1.复制整个ueditor文件夹到项目任意目录中. 2.添加Web窗体,并按以下方式引用配置和源文件,并实例 ...
- [团队项目]第二个冲刺 看板和燃尽图 Sprint2 6.8/6.9/6.10/6.11/6.12/6.13/6.14
1.开始一个新的冲刺: 起止:2016.6.1~2016.6.14 按照以下过程进行 ProductBacklog:继续向下细化 Sprint 计划会议:确定此次冲刺要完成的目标 Sprint Bac ...
- JDBC的批量处理数据
主要用到的方法有: preparedStatement.executeBatch();//积攒的数据执行 preparedStatement.clearBatch();//积攒的清除掉 prepare ...
- 【转】Linus:利用二级指针删除单向链表
原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...
- 局域网无法访问vmware虚拟机WEB服务器解决办法
环境:虚拟机服务器是centos,apache+php+mysql环境,但是局域网无法访问 1.本机能ping通虚拟机 2.虚拟机也能ping通本机 3.虚拟机能访问自己的web 4.本机无法访问虚拟 ...
- MFC设置对话框大小
设置对话框大小不可改变 1.在类的头文件中加入函数申明 (.h文件) afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI); 2.在消息映射中添加 (.cpp ...
- AB串(上帝都不会,我就没救了)
[题目分析] 设答案的长度为m,
- 集合、ArrayList 集合。Stack集合。Queue集合。以及Hashtable集合
arrayList 首先复制Colections加 : 创建arrayList ar =new arrayList(); //ArrayList al=new ArrayList(); ...