2013-08-04 22 views
0

我在一個名爲SIAlertView的開源庫中遇到了一些代碼,我不確定它在做什麼?特別是下面的兩行?什麼是SIAleartViewHandler,因爲它不是SIAlertView庫中的類?這是一個Objective-C塊嗎?

typedef void(^SIAlertViewHandler)(SIAlertView *alertView); 
@property (nonatomic, copy) SIAlertViewHandler willShowHandler; 

SIAlertView.h

typedef void(^SIAlertViewHandler)(SIAlertView *alertView); 
@property (nonatomic, copy) SIAlertViewHandler willShowHandler; 
@property (nonatomic, copy) SIAlertViewHandler didShowHandler; 
@property (nonatomic, copy) SIAlertViewHandler willDismissHandler; 
@property (nonatomic, copy) SIAlertViewHandler didDismissHandler; 

SIAlertView.m

[self transitionInCompletion:^{ 
    if (self.didShowHandler) { 
     self.didShowHandler(self); 
    } 
    [[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewDidShowNotification object:self userInfo:nil]; 

    [SIAlertView setAnimating:NO]; 

    NSInteger index = [[SIAlertView sharedQueue] indexOfObject:self]; 
    if (index < [SIAlertView sharedQueue].count - 1) { 
     [self dismissAnimated:YES cleanup:NO]; // dismiss to show next alert view 
    } 
}]; 

#pragma mark - SIAlertItem 

@interface SIAlertItem : NSObject 

@property (nonatomic, copy) NSString *title; 
@property (nonatomic, assign) SIAlertViewButtonType type; 
@property (nonatomic, copy) SIAlertViewHandler action; 

@end 

@implementation SIAlertItem 

@end 

回答

3

第一行(typedef void(^SIAlertViewHandler)(SIAlertView *alertView);)定義了類型的塊,稱爲SIAlertViewHandler

第二行(@property (nonatomic, copy) SIAlertViewHandler willShowHandler;)定義這是會屬性存儲該塊類型的實例。

+0

@interface SIAlertItem:NSObject是在另一個類.m文件中定義的嗎? – jdog

+0

這是一個類,就像任何其他類一樣,但只能從這個文件內部訪問(因爲沒有公共存在的定義)。 – Wain

1

是的,這是一個塊。

第一行是創建一個新的類型有點理智的添加到使用該塊(而不必重複void(^)(SIAlertView *alertView)無處不在,你可以使用SIAlertViewHandler

SIAlertView.m被檢查,看是否showHandler是置和,如果是,調用塊。