2011-05-10 72 views
2

這是我創建的警報:UIAlertView中clickedButtonAtIndex不叫

UIAlertView* dialog = [[UIAlertView alloc] init]; 

dialog.delegate = self; 

//some options 

aField = [[UITextField alloc]initWithFrame:CGRectMake(20.0,45.0,245.0,25.0)]; 
[aField setBackgroundColor:[UIColor whiteColor]]; 
[dialog addSubview:appkeyField]; 

[dialog show]; 
[aField release]; 

然而

- (void)alertView:(UIAlertView *)alertView 
        clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"test here"); 
} 

什麼都不做。日誌中沒有任何東西!什麼可能是錯的?

+1

你放置'UIAlertViewDelegate'協議在頭部? – 2011-05-10 18:06:57

+0

謝謝,這是概率,讓這個答案! – 2011-05-10 18:15:08

回答

3

當您設置對象的delegate屬性時,您的類必須採用適當的協議。在你的情況,您使用的是UIAlertView,所以你需要將UIAlertViewDelegate協議添加到您的頭文件:

@interface MyClass : UIViewController <UIAlertViewDelegate> 

要添加多個協議,使用:

@interface MyClass : UIViewController <Protocol1, Protocol2, ...>