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

如果你想自己做一个类别,上面就足够了。但是,有很多类使用委托模式。你想一一做分类吗?

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 回答