2010-11-30 40 views

回答

3

通常的做法是對每個UIAlertViewtag屬性使用唯一編號,然後檢查代理回調中的標記。一個簡單的方法就是使用一個枚舉:

enum { 
    kServiceErrorAlert = 1, 
    kFailedToSaveAlert = 2 
}; 

... 

alertView.tag = kServiceErrorAlert; 
[alertView show]; 
2

如果你能負擔得起的4.x版只能運行,你可以使用塊,而忘記了代表和標籤:

LambdaAlert *alert = [[LambdaAlert alloc] 
    initWithTitle:@"Test Alert" 
    message:@"See if the thing works."]; 
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }]; 
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }]; 
[alert addButtonWithTitle:@"Cancel" block:NULL]; 
[alert show]; 
[alert release]; 

LambdaAlert on GitHub

+0

在iPhone時間iOS 4.x已經出現了很長時間:)很好的答案 – 2010-11-30 08:28:31

相關問題