UIAlertView代理

20
有人能解释一下如何使用委托(delegate)来处理UIAlertView吗?是自动调用,还是我需要手动调用?例如: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

我编写了一个通用类,用于使用块回调替换UIAlertView委托。您可以在此处查看它:http://stavash.wordpress.com/2013/01/31/quick-tip-uialertview-with-a-block-callback/。 - Stavash
4个回答

32

假设你显示了一个委托为"self"的警报

- (void)showAlert {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                                                       message:@"Do you want to continue?"
                     delegate:self
                  cancelButtonTitle:nil
                  otherButtonTitles:@"No", @"Yes", nil];
        [myAlert show];
        [myAlert release];
}
为了让以下内容在你的 .m 文件中正常工作:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

你的 .h 文件需要在实现语句中引用 UIAlertViewDelegate,像这样:

@interface myViewController : UIViewController <UIAlertViewDelegate> {
}

这就是使你的 .m 文件能够响应 UIAlertViewDelegate 方法调用的原因。


2
将委托列在头文件中并非必需,但是这是一个好的实践。 - braden
为什么文档里没有提到这个!?https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: - Paweł Brewczynski

11

看起来我在init中没有调用delegate:self,感谢您的快速响应。 - Joe

10

这里有一个委托的包装器,使您可以使用块代替。执行流程将保持不变,但代码流程更容易理解。因此,用法如下:

[YUYesNoListener yesNoWithTitle:@"My Title" message:@"My Message" yesBlock:^
{
    NSLog(@"YES PRESSED!");
}
noBlock:^
{
    NSLog(@"NO PRESSED!");
}];

...这里是助手类:

typedef void(^EmptyBlockType)();

@interface YUYesNoListener : NSObject <UIAlertViewDelegate>

@property (nonatomic, retain) EmptyBlockType yesBlock;
@property (nonatomic, retain) EmptyBlockType noBlock;

+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock;

@end

@implementation YUYesNoListener

@synthesize yesBlock = _yesBlock;
@synthesize noBlock = _noBlock;

- (id) initWithYesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
    self = [super init];
    if (self)
    {
        self.yesBlock = [[yesBlock copy] autorelease];
        self.noBlock = [[noBlock copy] autorelease];
    }
    return self;
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0 && self.noBlock)
        self.noBlock();
    else if (buttonIndex == 1 && self.yesBlock)
        self.yesBlock();

    [_yesBlock release];
    [_noBlock release];
    [alertView release];
    [self release];
}

- (void) alertViewCancel:(UIAlertView *)alertView
{
    if (self.noBlock)
        self.noBlock();
    [_yesBlock release];
    [_noBlock release];
    [alertView release];
    [self release];
}

+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
    YUYesNoListener* yesNoListener = [[YUYesNoListener alloc] initWithYesBlock:yesBlock noBlock:noBlock];
    [[[UIAlertView alloc] initWithTitle:title message:message delegate:yesNoListener cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil] show];
}

@end

2

alertView:clickedButtonAtIndex: 方法是 UIAlertView 的代理自动调用的。 UIAlertView 的 init 方法需要一个代理作为参数之一。只需确保传入一个响应 alertView:clickedButtonAtIndex: 方法的对象即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接