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

发布于 2024-12-14 09:49:07 字数 319 浏览 2 评论 0原文

我看到了 Marco Armant 的这条推文

对按钮上的 UIActionSheet w/target:action:userInfo: 进行子类化以避免 delegate/buttonIndex。难道没有其他人这样做过吗?找不到。

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

I saw this tweet by Marco Armant:

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

I think that sounds like a great idea, but I wasn't able to find anyone's code that did this. Does anyone know of one, before I go do it myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-12-21 09:49:07

是的,请参阅我的 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
   }
 }];

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

Yes, see my github for OHActionSheet.

It is implemented using blocks, so that you can use it this way, even without deporting the target/action code elsewere in your source code, the great advantage being that everything is located in the same place in your source code, and that you can use as many OHActionSheets as you want in the same controller

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
   }
 }];

[EDIT] Edited sample code to add more details and usage examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文