我正在閱讀iOS 5推動限制,作者有一個基於塊的警報視圖的示例。下面的代碼:基於塊的UIAlertView與ARC
.H
typedef void (^DismissBlock)(int buttonIndex);
typedef void (^CancelBlock)();
+ (UIAlertView *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSArray *)otherButtons
onDismiss:(DismissBlock)dismissed
onCancel:(CancelBlock)cancelled;
.M
static DismissBlock _dismissBlock;
static CancelBlock _cancelBlock;
+ (UIAlertView *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSArray *)otherButtons
onDismiss:(DismissBlock)dismissed
onCancel:(CancelBlock)cancelled {
[_cancelBlock release];
_cancelBlock = [cancelled copy];
[_dismissBlock release];
_dismissBlock = [dismissed copy];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:[self self] cancelButtonTitle:cancelButtonTitle otherButtonTitles: nil];
for (NSString *buttonTitle in otherButtons) {
[alert addButtonWithTitle:buttonTitle];
}
[alert show];
return [alert autorelease];
}
+ (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
_cancelBlock();
}
else {
_dismissBlock(buttonIndex - 1); // cancel button is index 0
}
[_cancelBlock autorelease];
[_dismissBlock autorelease];
}
我不得不對此實施了幾個問題。
1)如果我使用ARC,在showAlertViewWithTitle方法中,在複製之前是否需要釋放塊?爲什麼或者爲什麼不?
2)在showAlertViewWithTitle:方法中,他分配委託:[self self]。這實際上是如何工作的?我以前沒有看過這個符號。
3)爲什麼要爲dismiss和cancel塊聲明靜態變量?這基本上是作爲這個類別的伊娃嗎?
謝謝!
爲什麼'_dismissBlock(buttonIndex - 1);'爲什麼不'_dismissBlock(buttonIndex);'? – anonim