为UIActionSheet和UIAlertView添加功能,支持使用block而非delegate来检查哪个按钮被点击
pod 'GKBlocks'
或者您可以使用subspecs来安装
pod 'GKBlocks/UIActionSheet'
pod 'GKBlocks/UIAlertView'
UIActionSheet和UIAlertView都扩展了2个初始化方法,这些方法接受一个block作为参数。对于UIActionSheet
GKActionSheetBlock block = ^(UIActionSheet *sheet, NSInteger buttonIndex) {
NSLog(@"CancelButton Pressed = %d", sheet.cancelButtonIndex == buttonIndex);
};
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Test" block:block cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"OK", nil];
[sheet showInView:self.view];
对于UIAlertView
GKAlertViewBlock block = ^(UIAlertView *alertView, NSInteger buttonIndex) {
NSLog(@"CancelButton Pressed = %d", alertView.cancelButtonIndex == buttonIndex);
};
[[[UIAlertView alloc] initWithTitle:@"Test" message:@"This is a test" block:block cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil] show];
Georg Kitz, @gekitz