Ios6 如何在没有委派的情况下从UIAlertView完成块访问用户输入?

Ios6 如何在没有委派的情况下从UIAlertView完成块访问用户输入?,ios6,uitextfield,uialertview,objective-c-blocks,objective-c-category,Ios6,Uitextfield,Uialertview,Objective C Blocks,Objective C Category,使用iOS6: 我想将用户输入的文本检索到与UIAlertView关联的UITextField中。我知道我可以用一个委托来实现期望的结果,但是我很好奇用回调函数来解决这个问题,因为我相信这可能是一个有趣的模式。我首先研究了UIAlertView类的类别扩展的通用模式。代码如下。提前感谢您的建议 import <UIKit/UIKit.h> @interface UIAlertView (Block) - (id)initWithTitle:(NSString *)title mes

使用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

因此,基本上您希望从块访问警报视图。您可以这样做:

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

因此,基本上您希望从块访问警报视图。您可以这样做:

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

如果你想自己做一个分类,以上就足够了。 但是,有许多类使用委托模式。你想一个接一个地分类吗

有。使用它,您可以像使用基于块的类一样使用这些类:

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;

试试这个库,这里是另一个有用的库

试试这个库这里是另一个有用的库,可以做同样的事情

谢谢,我相信这将允许我通过完成块访问内部对象。我会做一些测试来确认。对于其他阅读本文的人来说,由于排序的原因,这有点违反直觉,但我刚刚确认这确实有效。谢谢,我相信这将允许我通过完成块访问内部对象。我会做一些测试来确认。对于其他阅读本文的人来说,这有点违反直觉,因为排序问题,但我刚刚确认这确实有效。