gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:11:42 24 4
gpt4 key购买 nike

我看到了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com