2010-09-29 27 views
19

Ai đó có thể giải thích cách thức người được ủy quyền tham gia UIAlertView hoạt động? Nó được gọi tự động hay tôi phải gọi nó? Ví dụ:UIAlertView Delegates

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

+0

Tôi đã viết một lớp chung cho thay thế đoàn UIAlertView với callbacks khối. Bạn có thể kiểm tra nó ra [ở đây] (http://stavash.wordpress.com/2013/01/31/quick-tip-uialertview-with-a-block-callback/). – Stavash

Trả lời

11

Vì vậy, miễn là bạn đang đặt một cách chính xác tài sản đại biểu của UIAlertView và thực hiện các giao thức, nó sẽ tự động gọi khi người dùng nhấp vào một nút trong cảnh báo của bạn.

Hãy xem các dự án được liệt kê trong "Mã mẫu có liên quan" tại http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html để xem nó hoạt động.

+0

trông giống như tôi đã không gọi đại biểu: tự trong init, cảm ơn cho te phản ứng nhanh chóng – Joe

2

Phương thức alertView:clickedButtonAtIndex: của đại biểu được tự động gọi là UIAlertView. Phương thức init cho UIAlertView lấy một đại biểu làm một trong các tham số. Chỉ cần chắc chắn để vượt qua trong một đối tượng phản ứng với alertView:clickedButtonAtIndex:.

32

Hãy nói rằng bạn thấy một cảnh báo mà các đại biểu đã "tự"

- (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]; 
} 

Để cho phần sau đây để làm việc trong tập tin .m của bạn:

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

tập tin .h của bạn sẽ cần để tham chiếu UIAlertViewDelegate trong tuyên bố triển khai như vậy:

@interface myViewController : UIViewController <UIAlertViewDelegate> { 
} 

Đây là điều cho phép y tệp .m của chúng tôi để trả lời các cuộc gọi phương thức UIAlertViewDelegate.

+2

Có đại biểu được liệt kê trong tiêu đề là không cần thiết, nhưng là thực hành tốt – braden

+0

Tại sao điều này không được nói trong tài liệu này !? https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: –

10

Dưới đây là trình bao bọc cho đại biểu để bạn có thể sử dụng các khối thay thế. Luồng thực thi sẽ giống nhau nhưng luồng của mã sẽ dễ theo dõi hơn. Vì vậy, sử dụng:

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

... và đây là lớp helper:

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