如何在没有委托的情况下从UIAlertView完成块访问用户输入? [英] How to access user input from UIAlertView completion block without delegation?

查看:118
本文介绍了如何在没有委托的情况下从UIAlertView完成块访问用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iOS6:

我想将用户输入的文本检索到与UIAlertView关联的UITextField中。我知道我可以通过委托获得理想的结果但是我很好奇用回调函数来解决这个问题,因为我相信这可能是一个有趣的模式。我首先检查了UIAlertView类的类扩展的常见模式。代码如下。在此先感谢您的任何建议。

I would like to retrieve the text entered by a user into a UITextField associated with the UIAlertView. I am aware that I could achieve the desired result with a delegate however I am curious about solving this issue with a callback function as I believe this may be an interesting pattern. I began by examining a common pattern for category extension of the UIAlertView class. Code below. Thanks in advance for any suggestions.

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。

Usage for the category is set below. The main problem is my inability to reference the UIAlertView textField at Index 0 from within the completion block.

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


推荐答案

所以基本上你想要从中查看警报视图块。你可以这样做:

So basically you want to access the alert view from the block. You can do something like this:

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

这篇关于如何在没有委托的情况下从UIAlertView完成块访问用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆