UIAlertView代表

Joe*_*Joe 20 iphone cocoa-touch objective-c

有人可以解释代表如何UIAlertView工作?是自动调用还是必须调用它?例如:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

dre*_*ful 33

假设您显示了代表是"自我"的警报

- (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];
}
Run Code Online (Sandbox Code Playgroud)

为了使以下内容适用于.m文件:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Run Code Online (Sandbox Code Playgroud)

您的.h文件需要在实现语句中引用UIAlertViewDelegate,如下所示:

@interface myViewController : UIViewController <UIAlertViewDelegate> {
}
Run Code Online (Sandbox Code Playgroud)

这是允许.m文件响应UIAlertViewDelegate方法调用的原因.

  • 将标题列在标题中是没有必要的,但这是一种很好的做法 (2认同)

Kri*_*kel 11

只要您正确设置UIAlertView的委托属性并实现协议,当用户单击警报中的按钮时,它将自动调用.

Take a look at the projects listed under "Related sample code" at http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html to see it in action.


Ric*_*son 10

这是委托的包装器,以便您可以使用块.执行流程将是相同的,但代码流将更容易遵循.所以,用法:

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

......这里是助手类:

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
Run Code Online (Sandbox Code Playgroud)