自定义UIAlertView无委托回调



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

我已经将所有必要的文件导入到我的应用程序中,并且编译得很好。现在的问题是,我该如何处理逻辑变化?

所以在使用苹果的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,就没有回调哪个按钮被按下了,他们处理按钮按下的方式是将要执行的代码放在块中,如下所示。不管怎样,类似的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];

现在我不知道该怎么办,在这个区块里,我如何实现我以前使用苹果原生UIAlertView所做的?我无法访问按钮索引,也无法访问按钮的名称(无论如何,这对我的情况都没有帮助,因为我需要索引)

无论如何,我应该如何继续做我对苹果原生UIAlertView所做的事情,但对BlockAlertView的逻辑所做的?

谢谢!

编辑1@Christian Pappenberger:

这是BlockAlertView的.h,除非我错了,否则我不认为有任何协议可以添加。这是:

@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

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

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

上面的代码向BlockAlertView添加了一个按钮,一旦按下,中的代码就是要执行的代码。这里有一个例子:

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

一旦执行代码并显示alertView,alertView中将出现两个按钮,标题分别为"第一个按钮"one_answers"第二个按钮"。当您单击每个按钮时,将执行块中的代码。控制台将根据按下的按钮输出"按下第一个按钮"或"按下第二个按钮"。

既然你知道了这种类型的alertViews是如何工作的,我将解释你在这种情况下需要做什么。

正如你所指出的,你不会得到buttonIndex,但你会知道是哪个按钮触发了块。

因此,如果你现在需要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范例,当按下按钮时,它将在UIAlerViewDelegate的情况下调用委托方法(clickedButtonAtIndex:)。当一个块被执行时,每个单独的块都会被触发。

关键是:

当您使用块方法时,每个按钮都拥有触发后将执行的代码。在委托方法中,当按下按钮时,它们将调用一个要执行的通用方法。

举个例子:

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

代理方法:

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");
}

正如您所看到的,一旦按下按钮,就会调用委托方法,然后根据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];

在这种情况下,没有调用委托方法,执行代码在每个按钮内!因此,您不需要"检查特定的按钮标题字符串并根据它编写代码"。您需要包含将在每个按钮中执行的代码。

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

是否检查了是否有协议实现?