ios - 有人让 UIActionSheet (buttonIndex, delegate) 更像 UIButton (addTarget :action)?

标签 ios uibutton uiactionsheet

我看到了this tweet作者:Marco Armant:

Subclassed UIActionSheet w/target:action:userInfo: on buttons to avoid delegates/buttonIndex. Didn't someone else do this? Can't find it.

我认为这听起来是个好主意,但我找不到任何人的代码来执行此操作。在我自己去做之前,有人知道吗?

最佳答案

是的,查看我的 github OHActionSheet .

它是使用 block 实现的,所以你可以这样使用它,即使没有在你的源代码中驱逐目标/ Action 代码,最大的好处是所有的东西都位于你的源代码中的同一个地方,并且您可以在同一 Controller 中使用任意数量的 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
   }
 }];

[编辑] 编辑示例代码以添加更多详细信息和使用示例

关于ios - 有人让 UIActionSheet (buttonIndex, delegate) 更像 UIButton (addTarget :action)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8067804/

相关文章:

ios - 自动续订订阅和非消耗性 IAP

php - 仅适用于 iPad 的 Safari 浏览器中的损坏站点

ios - 如何以编程方式创建多个按钮和操作?

ios - 如何添加 UIActionSheet 按钮复选标记?

ios - 如何从 UIactionsheet 中的 UISwitch 获取答案

ios - 如何从 web 获取 base64 并在 Swift 中解码?

ios - 我如何观察 ReactiveSwift 中 UIControl 子类的变化?

iphone - 增加 UIButton 高亮时的触摸效果半径

swift - 将 CAShapeLayer 添加到 UIButton 不起作用

ios - 类似于 iOS 7 Safari 的类似 UIActionSheet 的控件