0

最近実装しました: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が押されたコールバックがないため、ボタンの押下を処理する方法は、以下に示すように、実行するコードをブロックに配置することです。とにかく、これは同様の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];

これが私が何をすべきかわからないことです。そのブロックで、AppleのネイティブUIAlertViewで以前に行ったことをどのように達成しますか?ボタンのインデックスにアクセスできず、ボタンの名前にもアクセスできません(インデックスが必要なため、私の場合はそうはいきません)

とにかく、Appleのネイティブ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
4

2 に答える 2

3

ブロックは、トリガーされると実行されるコードの一部です。

[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に「FirstButton」と「SecondButton」というタイトルの2つのボタンが表示されます。各ボタンをクリックすると、ブロック内のコードが実行されます。コンソールは、押されたボタンに応じて、「最初に押されたボタン」または「2番目に押されたボタン」を出力します。

このタイプの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ボタンが押されたときのパラダイムでは、ケースのデリゲートメソッド( clickedButtonAtIndex:)が呼び出されます。UIAlerViewDelegateブロックが実行されると、個別のブロックがトリガーされます。

キーは次のとおりです。

ブロックアプローチを使用する場合、各ボタンはコードを所有し、トリガーされると実行されます。デリゲートアプローチでは、ボタンが押されると、実行される共通のメソッドが呼び出されます。

したがって、例では:

目標:押されたボタンに応じて、コンソールに異なる単語を出力します。

デリゲートアプローチ:

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

この場合、デリゲートメソッドは呼び出されず、実行コードは各ボタン内にあります。したがって、「特定のボタンのタイトル文字列をチェックし、それに基づいてコードを実行する」必要はありません。各ボタンで実行されるコードを含める必要があります。

はっきりしているのかわかりません。さらに説明が必要な場合は、お気軽にお問い合わせください。

于 2012-08-05T21:05:58.377 に答える
0

プロトコルの実装者がいるかどうかを確認しましたか?

于 2012-08-05T20:53:28.433 に答える