---
title: "IOS之操作表ActionSheet（免Delegate）"
author: "ikrboy"
date: "2014-01-12T13:49:19Z"
tags: []
categories: ["移动开发"]
slug: "ios-actionsheet-delegate"
draft: false
lastmod: "2014-01-13T00:53:24Z"
id: 2002996
url: "https://ikrboy.iteye.com/blog/2002996"
---

接下来的操作，也是避免了Delegate的写法，方便对不同ActionSheet的分层操作。

1.添加头文件。文件可在附件下载。具体资料请参考：[https://github.com/emenegro/action-sheet-blocks#readme](https://github.com/emenegro/action-sheet-blocks#readme "https://github.com/emenegro/action-sheet-blocks#readme")

```
#include "UIActionSheet+Blocks.h"
```

 2.添加如下的方法。（自定义UIButton的单击触发事件）

```
- (IBAction)showActionSheet:(UIButton *)sender forEvent:(UIEvent *)event {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Test"
                                                       delegate:nil // Can be another value but will be overridden when showing with handler.
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:@"Delete"
                                              otherButtonTitles:@"Option 1", @"Option 2", nil];
	[sheet showInView:self.view
              handler:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
                  if (buttonIndex == [actionSheet cancelButtonIndex]) {
                      NSLog(@"Cancel button index tapped");
                  } else if (buttonIndex == [actionSheet destructiveButtonIndex]) {
                      NSLog(@"Destructive button index tapped");
                  } else  {
                      NSLog(@"Button %i tapped", buttonIndex);
                  }                      
              }];
	sheet.actionSheetStyle =  UIActionSheetStyleAutomatic;
	[sheet showInView:self.view];
}
```

   
![](http://dl2.iteye.com/upload/attachment/0093/0702/623458de-f8e2-3851-ba24-e8cf5e0dc827.png)