1

iOS6 を使用する場合:

ユーザーが入力したテキストを、UIAlertView に関連付けられた UITextField に取得したいと考えています。デリゲートを使用して目的の結果を達成できることは承知していますが、これは興味深いパターンである可能性があるため、コールバック関数を使用してこの問題を解決することに興味があります。まず、UIAlertView クラスのカテゴリ拡張の一般的なパターンを調べることから始めました。以下のコード。ご提案いただきありがとうございます。

import <UIKit/UIKit.h>

@interface UIAlertView (Block)
- (id)initWithTitle:(NSString *)title message:(NSString *)message completion:(void (^)(BOOL cancelled,     NSInteger buttonIndex, UITextField *textField))completion cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

@end

カテゴリの .m は次のとおりです。

#import "UIAlertView+Block.h"
#import <objc/runtime.h>


static char const * const alertCompletionBlockTag = "alertCompletionBlock";

@implementation UIAlertView (Block)

- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
         completion:(void (^)(BOOL cancelled, NSInteger buttonIndex))completion
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles, ... {

self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil ];


if (self) {
    objc_setAssociatedObject(self, alertCompletionBlockTag, completion, OBJC_ASSOCIATION_COPY);

    va_list _arguments;
    va_start(_arguments, otherButtonTitles);

    for (NSString *key = otherButtonTitles; key != nil; key = (__bridge NSString *)va_arg(_arguments, void *)) {
        [self addButtonWithTitle:key];
    }
    va_end(_arguments);
}

[self setAlertViewStyle:UIAlertViewStylePlainTextInput];
return self;
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

id completion = objc_getAssociatedObject(self, alertCompletionBlockTag);

[self complete:completion index:buttonIndex];
}

- (void) complete:(void (^)(BOOL cancelled, NSInteger buttonIndex))block index:(NSInteger)buttonIndex {
BOOL _cancelled = (buttonIndex == self.cancelButtonIndex);
block(_cancelled, buttonIndex );

objc_setAssociatedObject(self, alertCompletionBlockTag, nil, OBJC_ASSOCIATION_COPY);
//objc_removeAssociatedObjects(block);
}


@end

カテゴリの使用方法は以下に設定されています。主な問題は、完了ブロック内からインデックス 0 の UIAlertView textField を参照できないことです。

[[[UIAlertView alloc] initWithTitle:@"Add"
                           message:@"Add New Asset Type"
                        completion:^(BOOL cancelled, NSInteger buttonIndex){
                            if (!cancelled) {

                                //call on completion of UISheetAction ???
                                NSLog(@"%@",needToAccessUIAlertView._textFields[0]);



                            }
                        }
                 cancelButtonTitle:@"Cancel"
                 otherButtonTitles:@"OK", nil] show];
4

3 に答える 3

5

したがって、基本的には、ブロックからアラート ビューにアクセスする必要があります。次のようなことができます。

__block __weak UIAlertView *alertViewWeak;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Add"
                           message:@"Add New Asset Type"
                        completion:^(BOOL cancelled, NSInteger buttonIndex){
                            if (!cancelled) {

                                //call on completion of UISheetAction ???
                                NSLog(@"%@",[alertViewWeak textFieldAtIndex:0]);



                            }
                        }
                 cancelButtonTitle:@"Cancel"
                 otherButtonTitles:@"OK", nil];
alertViewWeak = alertView;
[alertView show];
于 2013-02-03T08:39:22.527 に答える
1

自分でカテゴリを作りたい場合は、上記で十分です。ただし、委任パターンを使用するクラスは多数あります。カテゴリを 1 つずつ作成しますか?

REKitがあります。これにより、そのクラスをブロックベースであるかのように使用できます。

UIAlertView *alertView;
alertView = [[UIAlertView alloc]
    initWithTitle:@"title"
    message:@"message"
    delegate:nil
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK", nil
];
[alertView
    respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)
    withKey:nil
    usingBlock:^(id receiver, UIAlertView *alertView, NSInteger buttonIndex) {
        // Do something…
    }
];
alertView.delegate = alertView;
于 2013-02-21T06:27:31.783 に答える
0

このライブラリを試す 同じことを行うための別の便利なライブラリを次に示します。http://ichathan.com/2014/08/19/ichalertview/

于 2014-08-25T08:54:34.313 に答える