2010-08-16 54 views
22

我試圖在按下按鈕時調用併發出警報。我使用這個:使用UIAlertView檢測按鈕單擊

-(IBAction)Add { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed" 
                message:@"Add to record" 
                delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil ]; 
    [alert show]; 
    [alert release];  
} 

好的,這裏沒問題,兩個按鈕上來,確定並取消。現在我想檢測哪個按鈕被按下我使用:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 0) 
    { 
     //just to show its working, i call another alert view 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 
                 message:@"no error"                           delegate:nil 
               cancelButtonTitle:@"IWORKS" 
               otherButtonTitles:@"NO PRB", nil]; 
     [alert show]; 
     [alert release];  


    } 
    else 
    { 
     NSLog(@"cancel");  
    } 
} 

現在,這是問題。我無法檢測到哪個按鈕被按下;第二個alertview不顯示。我已經通過代碼檢查了幾次,似乎沒有任何問題。沒有錯誤/警告。

+2

在另一個提醒後直接顯示提醒可能不是一個好主意 - 只是一個提示。 – vakio 2010-08-16 08:56:22

回答

32

要檢測按鈕點擊,警報視圖必須具有關聯的委託人,例如,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed" 
               message:@"Add to record" 
               delegate:self // <------ 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 
+2

+1:我寫作的一半是什麼。我還要提到視圖控制器需要實現'UIAlertViewDelegate'(儘管我相信它會給出一個警告,如果不是的話)。 – sdolan 2010-08-16 08:57:11

7

0的buttonIndex是取消按鈕。我會建議使用:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex == 0) 
{ 
     NSLog(@"cancel"); 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];                            
    [alert show]; 
    [alert release];   
} 
} 
+2

或者更好,'if(buttonIndex == alertView.cancelButtonIndex)...' – mojuba 2013-11-22 10:53:02

13

這是你的代碼,我使用並添加了一些我的代碼也。 **

-(IBAction) Add 
{ 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed" 

                 message:@"Add to record" 
                 delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

     alert.tag=101;//add tag to alert 
     [alert show]; 
     [alert release];  
} 

現在,當您按下警報按鈕,它會調用clickedButtonAtIndex 但應該爲每個警報一個標識符。所以添加標籤,然後

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex** 

{ 

// the user clicked one of the OK/Cancel buttons 
if(alert.tag=101)  // check alert by tag 
{  

if (buttonIndex == 0) 

    { 

    //just to show its working, i call another alert view 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 

                message:@"no error" 
                delegate:nil 
              cancelButtonTitle:@"IWORKS" 
              otherButtonTitles:@"NO PRB", nil]; 

    [alert show]; 
    [alert release];  

} 

else 

{ 
    NSLog(@"cancel");  
} 
} 
} 

希望它有幫助。

2

我覺得,如果你想顯示在現有的警報視圖按鈕點擊事件的新警報視圖,這將是更好地使用

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 

} 

委託方法,而不是

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

} 
2
1) 
.h file 
@interface MyClassViewController:<UIAlertViewDelegate> 

2) 
.m file 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" 
               message:@"some message" 
               delegate:self // must be self to call clickedButtonAtIndex 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 


3) 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 
    NSLog(@"The cancel button was clicked from alertView"); 
    } 
else { 

} 
}