1

我創建了一個UIAlertView,現在我想檢查用戶按下哪個按鈕。用戶按下我的UIAlertView上的哪個按鈕?

我的代碼是:

- (IBAction)button1 { 
    { 
     UIAlertView *alert1 = [[UIAlertView alloc] init]; 
     [alert1 setTitle:@"Hello"]; 
     [alert1 setMessage:@"Do you like smoking?"]; 
     [alert1 addButtonWithTitle:@"Yes"]; 
     [alert1 addButtonWithTitle:@"No"]; 
     [alert1 show]; 
    } 
} 

如何我if-else語句檢查呢?

+0

委託方法。 – Larme

回答

2

實現您的視圖控制器UIAlertViewDelegate協議,將其設置爲代表對UIAlertView,並等待alertView:clickedButtonAtIndex:事件,像這樣:

@interface MyViewController : UIViewController <UIAlertViewDelegate> 
-(void)alertView:(UIAlertView *)alertView 
     clickedButtonAtIndex:(NSInteger)buttonIndex; 
@end 

@implementation MyViewController 

-(void)alertView:(UIAlertView *)alertView 
    clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSLog(@"Button %d clicked...", (int)buttonIndex); 
} 

@end 

更改顯示警報視圖的代碼如下:

UIAlertView *alert1 = [[UIAlertView alloc] init]; 
[alert1 setTitle:@"Hello"]; 
[alert1 setMessage:@"Do you like smoking?"]; 
[alert1 addButtonWithTitle:@"Yes"]; 
[alert1 addButtonWithTitle:@"No"]; 
alert1.delegate = self; // <<== Add this line 
[alert1 show]; 
0

你能做的最好的事情是讓呈現您的警報視圖符合UIAlertViewDelegate協議和實現方法–alertView:clickedButtonAtIndex:類。這樣你就會知道按鈕被點擊了。

希望這會有所幫助!

4

您必須在UIAlertView的代表設置將從UIAlertView處理回調本身的類

例如

[alert1 setDelegate:self]; 

哪裏self是您當前UIViewController實現該協議<UIAlertViewDelegate>

當用戶點擊一個按鈕時,UIAlertView將回調您設置爲代表的任何對象,在我的示例中,我們使用的是創建UIAlertViewUIViewController。一旦我們得到回調,我們可以檢查哪個按鈕索引已被輕敲,並採取相應的行動。這是在iOS開發中使用的基本委託模式,尤其是UIKit。

@interface MyViewController : UIViewController() <UIAlertViewDelegate> 
@end 

@implementation MyViewController 

- (IBAction)button1 { 
    { 
     UIAlertView *alert1 = [[UIAlertView alloc] init]; 
     [alert1 setTitle:@"Hello"]; 
     [alert1 setMessage:@"Do you like smoking?"]; 
     [alert1 addButtonWithTitle:@"Yes"]; 
     [alert1 addButtonWithTitle:@"No"]; 
     [alert1 setDelegate:self]; 
     [alert1 show]; 
    } 
} 

#pragma mark - UIAlertViewDelegate 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // Handle interaction 
     switch (buttonIndex) 
     { 
      case 0: 
      NSLog(@"Yes was pressed"); 
      break; 
      case 1: 
      NSLog(@"No was pressed"); 
      break; 
     } 
} 

@end 

這是非常重要的,你在頭文件中指定,或者你的類實現了<UIAlertViewDelegate>類接口擴展。

我建議您參閱UIAlertViewDelegate Protocol Reference以獲取更多信息,並且您可以按照此方法處理其他許多UIKit組件。

+0

我知道這是一箇舊的,但即使是我發現的一些代碼是舊的不起作用。 iOS 8.1和Xcode 6.1已經過測試和工作! :)雖然我個人不得不添加兩個if,但仍然使用它,並且運行得非常好!謝謝!!! –

+0

很高興聽到它。儘管你現在應該考慮轉移到'UIAlertController'。 – Tim

+0

昨天在翻閱Apple Docs時看到了這個消息。這仍然適用於它嗎? –

1

加上傑夫的答案,我認爲你必須啓用另一個按鈕,以便點擊按鈕時放置你的邏輯。

爲了你的代碼創建按鈕:

- (IBAction)button1 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" 
                 message:@"Do you like smoking?" 
                 delegate:self 
               cancelButtonTitle:@"Yes" 
               otherButtonTitles:@"No", nil]; 
    [alert show]; 
} 

但我們知道當點擊該按鈕之前,您必須啓用第一其它按鈕,通過調用此委託方法:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 
    return YES; 
} 

這將啓用您創建的NO按鈕。然後,你將能夠用clickedButtonAtIndex方法做一些邏輯,我想你會這樣做。

落實UIAlertView中的委託方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     // i love smoking! 
    } else if (buttonIndex == 1) { 
     // i hate smoking! 
    } 
} 

一定要申報UIAlertViewDelegate在你的頭類。

確保alertViewShouldEnableFirstOtherButton:方法返回YES,否則無法在按下按鈕時放入邏輯。

希望這會有所幫助! :)

+0

謝謝你的代碼完全工作只有這一個工作,只有這個代碼不速率:) –