2012-08-05 58 views
0

我刚刚实施:https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets自定义UIAlertView无委托回调?

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

因此,与苹果公司的UIAlertView中之前,我做了这样的事情:在alertview的回调

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

然后,我会做到这一点:

- (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的逻辑吗?

谢谢!

EDIT1 @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 

回答

3

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

[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,具有标题“第一按钮”和“第二个按钮”。当你点击每个按钮时,会发生什么是块中的代码将被执行。控制台将根据按下的按钮输出“按下第一个按钮”或“第二个按钮”。

既然您已经知道这种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]; 

如果你需要的其他东西随便问进一步的解释。

EDIT加解释

block范例与委托模式不同。当按下按钮时,它将调用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]; 

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

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

+0

感谢,似乎工作。也只是如此,我知道未来,我将如何执行某些代码取决于特定的按钮标题,但我不确定它的索引?例如:在clickedButtonAtIndex中,我可以检查特定的按钮标题字符串,并根据它进行编码。我不确定在这种情况下我该如何做到这一点。 – 2012-08-05 21:24:32

+0

我编辑了我的答案,所以我可以添加更多的代码 – tomidelucca 2012-08-05 21:43:12

+0

对不起,我想我不清楚当问你这个问题! :/无论如何,让我重新修饰它。所以可以说我们有一个包含NSMutableDictionary的NSArray。字典中有一个名为“Color”的关键字,阵列中有4个NSMutableDictionarys,总体上有颜色:蓝色,橙色,红色,绿色。所以然后我通过数组使用for循环(就像我用myArray做的那样),在那里添加一个标题为“Color”中的NSString的按钮。所以现在我们添加了所有的按钮,我想在按下“Red”时执行某些代码。有了UIAlertView,我会通过检查 – 2012-08-05 21:55:14

0

你检查是否有协议实现?

+0

检查我的edit1我添加了文件的.h文件。 – 2012-08-05 20:57:33

+0

你是对的...但这种方法:dismissWithClickedButtonIndex是你正在寻找的。你知道如何实现一个协议吗? – 2012-08-05 23:54:50

+0

是的,但我的问题现在已经改变,看看我为其他回答者所做的最后2条评论。这是我的最后一个问题。 – 2012-08-06 00:19:54