ios - 自定义 UIAlertView 没有委托(delegate)回调?

标签 ios callback indexing custom-controls uialertview

我最近实现了:https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets

我已将所有必需的文件导入到我的应用程序中,并且可以正常编译。现在的问题是,我该如何处理逻辑变化?

所以在使用 Apple 的 UIAlertView 之前,我做了这样的事情:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Which Key?" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
            for (NSMutableDictionary *dict in myArray) {
                [alertView addButtonWithTitle:[dict objectForKey:@"Key"]];
            }
 [alertView show];
 [alertView release];

然后在 alertview 的回调中我会这样做:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    [[Singleton sharedSingleton] setKey:buttonIndex];
}

现在 BlockAlertView 没有按下哪个按钮的回调,它们处理按钮按下的方式是将您要执行的代码放入 block 中,如下所示。无论如何,这就是一个类似的 BlockAlertView 的样子:

BlockAlertView *alertView = [BlockAlertView alertWithTitle:@"Which Key?" message:nil];
            for (NSMutableDictionary *dict in myArray) {
                [alertView addButtonWithTitle:[dict objectForKey:@"Key"] block:^{
                    //Not sure what to do here
                }];
            }
[alertView show];

现在我不知道该怎么做,在那个 block 中我如何实现我之前使用 Apple 的原生 UIAlertView 所做的?我无法访问按钮索引,也无法访问按钮的名称(因为我需要索引,所以这对我的情况没有帮助)

无论如何,我应该如何继续使用 Apple 的原生 UIAlertView 但使用 BlockAlertView 的逻辑?

谢谢!

Edit1 @Christian Pappenberger:

这是 BlockAlertView 的 .h,我认为除非我错了,否则没有我可以添加的协议(protocol)。在这里:

@interface BlockAlertView : NSObject {
@protected
    UIView *_view;
    NSMutableArray *_blocks;
    CGFloat _height;
}

+ (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message;

- (id)initWithTitle:(NSString *)title message:(NSString *)message;

- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block;
- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block;
- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block;

- (void)show;
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

@property (nonatomic, retain) UIImage *backgroundImage;
@property (nonatomic, readonly) UIView *view;
@property (nonatomic, readwrite) BOOL vignetteBackground;

@end

最佳答案

block 是一旦被触发就会执行的代码的一部分。

[alertView addButtonWithTitle:[dict objectForKey:@"Key"] block:^{
                    //Not sure what to do here
                }];

上面的代码向 BlockAlertView 添加了一个按钮,一旦它被按下,block 中的代码就会被执行。这是一个例子:

...

[alertView addButtonWithTitle:@"First Button" block:^{
                    NSLog(@"First Button Pressed");
                }];

[alertView addButtonWithTitle:@"Second Button" block:^{
                    NSLog(@"Second Button Pressed");
                }];
...

[alertView show];

执行代码并显示 alertView 后,alertView 中将出现两个按钮,标题为“First Button”和“Second Button”。当您单击每个按钮时,将执行 block 中的代码。控制台将根据按下的按钮输出“First Button Pressed”或“Second Button Pressed”。

现在您已经知道这种类型的 alertView 是如何工作的,我将解释您需要在您的案例中做什么。

正如您所指出的,您不会获得 buttonIndex,但您会知道哪个按钮触发了该 block 。

因此,如果您现在需要 buttonIndex,我会添加一个 int buttonIndex 并每次递增它,如下面的代码所示:

BlockAlertView *alertView = [BlockAlertView alertWithTitle:@"Which Key?" message:nil];

    int buttonIndex = 0; // HERE

    for (NSMutableDictionary *dict in myArray) {
       [alertView addButtonWithTitle:[dict objectForKey:@"Key"] block:^{
                [[Singleton sharedSingleton] setKey:buttonIndex]; // HERE
           }];

    buttonIndex++; // HERE
                    }

        [alertView show];

如果您需要进一步解释其他内容,请随时提出。

编辑 添加说明

block 范式与委托(delegate)范式不同。使用 delegate 范例,当按下按钮时,它将调用 UIAlerViewDelegate 情况下的委托(delegate)方法 (clickedButtonAtIndex:)。当一个 block 被执行时,每个单独的 block 都会被触发。

这是关键:

当您使用 block 方法时,每个按钮都拥有一旦被触发就会执行的代码。在委托(delegate)方法中,当按下按钮时,它们将调用要执行的通用方法。

举个例子:

目标:根据按下的按钮向控制台输出不同的词。

委托(delegate)方法:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Which Key?" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

[alertView addButtonWithTitle:"@Button 1"];
[alertView addButtonWithTitle:"@Button 2"];
[alertView addButtonWithTitle:"@Button 3"];

[alertView show];

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0)
       NSLog(@"Dog");
    else if (buttonIndex == 1)
       NSLog(@"Cat");
    else if (buttonIndex == 2)
       NSLog(@"Shark");
}

如您所见,一旦按下按钮,委托(delegate)方法就会被调用,此时您可以根据 buttonIndex 决定输出什么。

拦截方式:

BlockAlertView *alertView = [BlockAlertView alertWithTitle:@"Which Key?" message:nil];

[alertView addButtonWithTitle:@"Button 1" block:^{
                    NSLog(@"Dog");
                }];
[alertView addButtonWithTitle:@"Button 2" block:^{
                    NSLog(@"Cat");
                }];          
[alertView addButtonWithTitle:@"Button 3" block:^{
                    NSLog(@"Shark");
                }];

[alertView show];

在这种情况下没有调用委托(delegate)方法,执行代码在每个按钮内部!所以你不需要“检查特定的按钮标题字符串并根据它做代码”。您需要包含将在每个按钮中执行的代码。

我不知道我是否清楚,如果您需要进一步的解释,请随时询问。

关于ios - 自定义 UIAlertView 没有委托(delegate)回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819964/

相关文章:

ios - 向 Pod 添加静态框架。

ios - 使用 openURL 在 iOS 上打开 Facebook URL 会导致 Facebook 应用程序页面损坏

node.js - 将 API 所需的回调 hell 转换为同步样式

elasticsearch - ElasticSearch:如果索引的运行状况为 'red'是什么意思?

mysql - 不使用索引的简单 MySQL WHERE 查询

sql-server - 如果是 DATETIME 或 DATETIME2,为什么索引可能不会做太多事情,因为它们包含时间部分?

iphone - UIGraphicsGetImageFromCurrentImageContext 的线程安全 (CG) 版本?

ios - 无法从 Facebook 帐户工具包获取短信代码

java - java中的回调行为

ios - 回调违反控制反转