UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。

为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。然后再xib文件添加一个button,用来弹出sheet view。

1、首先在.h文件中实现协议

加代码的地方在@interface那行的最后添加<UIActionSheetDelegate>,协议相当于java里的接口,实现协议里的方法。

  1. @interface sheetviewViewController : UIViewController<UIActionSheetDelegate>
  2. @end

2、添加button,命名button为showSheetView.

3、为button建立Action映射,映射到.h文件上,事件类型为Action ,命名为showSheet。

4、在.m文件上添加点击事件代码

图的效果是这样的:

  1. - (IBAction)showSheet:(id)sender {
  2. UIActionSheet *actionSheet = [[UIActionSheet alloc]
  3. initWithTitle:@"title,nil时不显示"
  4. delegate:self
  5. cancelButtonTitle:@"取消"
  6. destructiveButtonTitle:@"确定"
  7. otherButtonTitles:@"第一项", @"第二项",nil];
  8. actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
  9. [actionSheet showInView:self.view];
  10. }

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式

参数解释:

cancelButtonTitle  destructiveButtonTitle是系统自动的两项。

otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。

[actionSheet showInView:self.view];这行语句的意思是在当前view显示Action sheet。当然还可以用其他方法显示Action sheet。

对应上面的图和代码,一目了然了把

5、接下来我们怎么相应Action Sheet的选项的事件呢?

实现协议里的方法。为了能看出点击Action sheet每一项的效果,我们加入UIAlertView来做信息显示。下面是封装的一个方法,传入对应的信息,在UIAlertView显示对应的信息。

  1. -(void)showAlert:(NSString *)msg {
  2. UIAlertView *alert = [[UIAlertView alloc]
  3. initWithTitle:@"Action Sheet选择项"
  4. message:msg
  5. delegate:self
  6. cancelButtonTitle:@"确定"
  7. otherButtonTitles: nil];
  8. [alert show];
  9. }

那相应被Action Sheet选项执行的代码如下:

  1. (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  2. {
  3. if (buttonIndex == 0) {
  4. [self showAlert:@"确定"];
  5. }else if (buttonIndex == 1) {
  6. [self showAlert:@"第一项"];
  7. }else if(buttonIndex == 2) {
  8. [self showAlert:@"第二项"];
  9. }else if(buttonIndex == 3) {
  10. [self showAlert:@"取消"];
  11. }
  12. }
  13. - (void)actionSheetCancel:(UIActionSheet *)actionSheet{
  14. }
  15. -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
  16. }
  17. -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
  18. }

可以看到 buttonIndex 是对应的项的索引。

看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,

比如永久性删除一条消息或图像时。如果你指定了一个销毁按钮他就会以红色高亮显示:

actionSheet.destructiveButtonIndex=1;

与导航栏类似,操作表单也支持三种风格 :

UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字

UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字

UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字

用法:

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式

我选sheet 里的第一项,显示如下:

6、注意事项

在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:

在showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。

UIActionSheet的使用的更多相关文章

  1. 弹框控件 UIAlertView UIActionSheet

    // 创建弹框 从底部弹出,一般用于危险操作 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"恭喜通关" ...

  2. UI第十三节——UIActionSheet

    - (void)viewDidLoad {    [super viewDidLoad];        UISwitch *swc = [[UISwitch alloc] initWithFrame ...

  3. 【代码笔记】iOS-清除图片缓存UIActionSheet

    一,效果图. 二,代码. RootViewController.m //点击任何处出现sheet -(void)touchesBegan:(NSSet *)touches withEvent:(UIE ...

  4. UIAlertView 与 UIActionSheet (提示用户)的使用方法

    UIAlertView 提示用户  帮助用户选择框 //    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"警 ...

  5. UI里的UIActionSheet按钮

    1.效果图:分别为有短信分享                                      无短信分享 -(void)viewDidLoad{ //添加按钮 UIButton *share ...

  6. UIAlertView、 UIActionSheet

    一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...

  7. IOS笔记之UIKit_UIAlertView、UIActionSheet

    //首先必须继承协议 @interface TRViewController : UIViewController<UIAlertViewDelegate,UIActionSheetDelega ...

  8. UIActionSheet 修改字体颜色

    -(void)willPresentActionSheet:(UIActionSheet *)actionSheet { SEL selector = NSSelectorFromString(@&q ...

  9. 封装同步的UIActionSheet

    封装同步的UIActionSheet 发问题 做 iOS 开发的同学想必都用过 UIActionSheet.UIActionSheet 可以弹出一个选择列表,让用户选择列表中的某一项操作.使用 UIA ...

随机推荐

  1. 使用jquery的imagecropper插件做用户头像上传 兼容移动端

    在移动端开发的过程中,或许会遇到对图片裁剪的问题.当然遇到问题问题,不管你想什么方法都是要进行解决的,哪怕是丑点,难看点,都得去解决掉. 图片裁剪的jquery插件有很多,我也测试过很多,不过大多数都 ...

  2. UBoot讲解和实践-------------讲解(二)

    UBoot  ARM移植之阶段二   理论篇 stage2: 1.初始化本阶段要使用的硬件设备: 通常包括两方面: 1)初始化至少一个串口,以便和终端用户进行I/O输出信息.              ...

  3. Java enum(枚举)的用法详解(转)

    用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...

  4. OBD K线抓包 III

    14230 HL激活, 5BPS又称 00  //电平激活 C1 33 F1 81 66  //14230的Enter命令 83 F1 11 C1 EF 8F C4 //回应了,一个命令就回应了... ...

  5. 关于mysql中int(1)中int后面的数字

    mysql在建表的时候int类型后的长度代表什么? 是该列允许存储值的最大宽度吗? 为什么我设置成int(1), 也一样能存10,100,1000呢.  当时我虽然知道int(1),这个长度1并不代表 ...

  6. 报错:org.eclipse.swt.SWTError: No more handles at org.eclipse.swt.SWT.error(SWT.java:4517)

    在Mars.Kepler的版本裡,時常會出現以下錯誤導致eclipse無法進行運作 Error.log org.eclipse.swt.SWTError: No more handles     at ...

  7. mysql 配置文件 value

    在xml配置文件中配置数据库utl时,要使用&的转义字符也就是& 例如:<property name="url" value="jdbc:mysql ...

  8. [转]Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/13/251101.html 本系列教程将详细介绍Struts 1.x的基本原理和使用方法, ...

  9. HTML学习开篇

    最近开的博客,一切都是从零开始,昨天刚写了java的开篇,今天写一写HTML开篇. 很多初学者都不太看重前端的学习,甚至鄙视前端,我刚开始时就这样.其实,要想成为一个真正的程序员,前端和后端都必须了解 ...

  10. 怎样在excel中添加下拉列表框

    用excel2013打开要编辑的工作表,例子是一个班级名单,可以看到政治面貌目前还没有填写   接着我们找一个空白处,依次写入政治面貌的可能选项: 群众.共青团员   然后选中“政治面貌”这一列,点击 ...