接下来的操作,也是避免了Delegate的写法,方便对不同ActionSheet的分层操作。
1.添加头文件。文件可在附件下载。具体资料请参考: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]; }