我看过Marco Armant的this tweet:
按钮上的子类
UIActionSheet w/target:action:userInfo:
以避免委托/ buttonIndex。别人没有这样做吗?找不到它。
我认为这听起来很棒,但我无法找到任何代码来执行此操作。在我自己去做之前,有没有人知道一个?
答案 0 :(得分:2)
是的,请参阅我的github OHActionSheet
。
它是使用块实现的,因此你可以这样使用它,即使没有驱逐源代码中的目标/操作代码,最大的好处是所有东西都位于源代码的同一个地方,并且你可以在同一个控制器中使用尽可能多的OHActionSheets
NSURL* anURL = ... // some URL (this is only as an example on using out-of-scope variables in blocks)
[OHActionSheet showSheetInView:yourView
title:@"Open this URL?"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:[NSArray arrayWithObjects:@"Open",@"Bookmark",nil]
completion:^(OHActionSheet* sheet,NSInteger buttonIndex) {
if (buttonIndex == sheet.cancelButtonIndex) {
NSLog(@"You cancelled");
} else {
NSLog(@"You choosed button %d",buttonIndex);
switch (buttonIndex-sheet.firstOtherButtonIndex) {
case 0: // Open
// here you can access the anURL variable even if this code is executed asynchrously, thanks to the magic of blocks!
[[UIApplication sharedApplication] openURL:anURL];
break;
case 1: // Bookmark
default:
// Here you can even embed another OHAlertView for example
[OHAlertView showAlertWithTitle:@"Wooops"
message:@"This feature is not available yet, sorry!"
cancelButton:@"Damn"
otherButtons:nil
onButtonTapped:nil]; // no need for a completion block here
break;
} // switch
}
}];
[编辑]编辑示例代码以添加更多详细信息和用法示例