2012-03-26 75 views
2

我創建了一個帶有兩個按鈕的UIAlertView實例,並且在我的類的接口文件(.h)中,我也設置了委託,但仍然無法在單擊按鈕時得到任何響應。這裏是我的代碼:UIAlertView委託方法不響應iOS 5.1

//myClass.h 
    @interface MainMenu : UIViewController<UIAlertViewDelegate> 
-(IBAction)secondAct:(id)sender; 

和實現

-(IBAction)secondAct:(id)sender 
     alert = [[UIAlertView alloc] initWithTitle:@"Dear User" 
                 message:@"Your Request Will be Sent To Security" 
                 delegate:nil 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"OK", nil]; 
    [alert show]; 
    [alert autorelease]; 
} 

和委託方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
NSLog(@"UIAlertView delegate works");//this line too isnt displayed 
NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ]; 
if ([title isEqualToString:@"OK"]) { 
NSLog(@"OK Pressed"); 
}//i want to create something like this 

} 我做了所有上面的代碼,但仍不能採取任何行動。無論我點擊哪個按鈕,都會使警報消失。這段代碼有什麼問題,任何人都可以幫忙? ANSWER

親愛PeterG.評論更改delegete:nildelegate:self和現在的工作。

+3

委託:無將無法正常工作...更改爲委託:自我 – 2012-03-26 14:00:40

+0

我曾經嘗試過。但它給出了一個錯誤 – ilhnctn 2012-03-26 14:01:28

+0

@PeterG。對不起兄弟,它工作 – ilhnctn 2012-03-26 14:02:45

回答

2

代表應該可以設置爲self。如果它在這裏發佈錯誤。

另外我不認爲你應該autorelease警報伊娃。嘗試跳過該行,看看會發生什麼?

+0

啊,你已經得到它的工作:P – Sunkas 2012-03-29 07:52:18

+0

在編輯我提到過。多謝兄弟:) – ilhnctn 2012-03-29 07:59:57

0

只給代表「自我」。

-(IBAction)secondAct:(id)sender 
     alert = [[UIAlertView alloc] initWithTitle:@"Dear User" 
                 message:@"Your Request Will be Sent To Security" 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"OK", nil]; 
    [alert show]; 
}