2014-02-06 34 views
-4

是否有可能根据不同的条件在同一个IBAction中用不同的按钮显示不同的警报?我在IBAction为一些代码:如何在一个IBAction中使用不同的按钮使用多个UIAlertView?

- (IBAction)btnRecommendationTapped:(id)sender { 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                 message: @"It seems that you are interested in the %d of this collection." 
                delegate: nil 
              cancelButtonTitle: nil 
              otherButtonTitles: @"Yes", @"No", nil]; 
[alert show]; 
} 

我想有另一个警报显示“!很抱歉,我们无法提供任何建议,”用确定按钮。当条件满足时,显示IBAction中的一个显示这个。

+3

'if(condition){create alertView} else {create other alertView}'?! –

+0

条件是什么?你听说过“if语句”吗? – Popeye

回答

2
-(IBAction)buttonClicked:(id)sender 
    { 
      if([sender tag]==0) 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"button 1 clicked" delegate:self 
      cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
     } 
     else if ([sender tag]==1) 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"button 2 clicked" delegate:self 
      cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
     } 
     else if ([sender tag]==2) 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"button 3 clicked" delegate:self 
      cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
     } 
} 
+1

公约说'ButtonClicked:'应该是'buttonClicked:' – Popeye

+0

@Popeye谢谢我编辑我的答案 –

0

尝试如下:

- (IBAction)btnRecommendationTapped:(id)sender { 
if(condition has fulfilled){ 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                 message: @"It seems that you are interested in the %d of this collection." 
                delegate: nil 
              cancelButtonTitle: nil 
              otherButtonTitles: @"Yes", @"No", nil]; 
[alert show]; 
} 
else{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Sorry! we are unable to provide any recommendation" 
                 message: nil" 
                delegate: nil 
              cancelButtonTitle: nil 
              otherButtonTitles: @"Ok", nil]; 
[alert show]; 

} 
} 
+0

如果不是你的答案是错误的,那么额外的'''是一个错字 – Popeye

1

有几种不同的方法可以做到这一点。您可以使用if statement,根据条件创建不同的UIAlertView

选项1:

- (IBAction)btnClicked:(id)sender 
{ 
    UIAlertView *ourAlertView; // Create the local variable, we don't need to create multiple ones we can just set to the one. 
    // This is your if statement to determine if your condition has been met 
    // Whatever that condition is here we're going to assume you have set a BOOL 
    // flag before hand called failed. 
    if(!failed) { 
     ourAlertView = [[UIAlertView alloc] initWithTitle:@"Recommendations" 
                message:@"We recommend..." 
               delegate:self // Very important 
             cancelButtonTitle:@"No" 
             otherButtonTitles:@"Yes", nil]; 
     [ourAlertView setTag:0]; // Would recommend setting a tag to determine which one has been used in the delegate methods. 
    } else { // else if() if you want more then 2 
     ourAlertView = [[UIAlertView alloc] initWithTitle:@"Sorry" 
                message:@"We're sorry..." 
               delegate:self // Very important 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
     [ourAlertView setTag:1]; 
    } 

    [ourAlertView show]; 
} 

第二种办法很简单,但我们会做以下,仍使用if statement但我们只建立一个UIAlertView

选项2:

- (IBAction)btnClicked:(id)sender 
{ 
    // We create an empty `UIAlertView` only setting the delegate to self. 
    UIAlertView *ourAlertView = [[UIAlertView alloc] initWithTitle:nil 
               message:nil 
               delegate:self // Very important 
            cancelButtonTitle:nil 
            otherButtonTitles:nil]; 

    if(!failed) { 
     [ourAlertView setTitle:@"Recommendations"]; 
     [ourAlertView setMessage:@"We recommend..."]; 
     [ourAlertView addButtonWithTitle:@"No"]; 
     [ourAlertView addButtonWithTitle:@"Yes"]; 
     [ourAlertView setCancelButtonIndex:0]; 
     [ourAlertView setTag:0]; 
    } else { 
     [ourAlertView setTitle:@"Sorry"]; 
     [ourAlertView setMessage:@"We're sorry..."]; 
     [ourAlertView addButtonWithTitle:@"OK"]; 
     [ourAlertView setCancelButtonIndex:0]; 
     [ourAlertView setTag:1]; 
    } 

    [ourAlertView show]; 
} 

但是最重要的需要的东西(那就是,如果你想让它正常工作),很多人忘记的是UIAlertViewDelegate

所以在你.h文件,你必须在@interface设置你需要做的

@interface MySubClass : MySuperClass <UIAlertViewDelegate> 

这将允许您使用以下方法从UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 
- (void)alertViewCancel:(UIAlertView *)alertView 
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *) 
- (void)didPresentAlertView:(UIAlertView *)alertView 
- (void)willPresentAlertView:(UIAlertView *)alertView 

结帐两个UIAlertView Apple DocumentationUIAlertViewDelegate Apple Documentation

非常重要否关于UIAlertView的苹果文档中有关子类化的问题。

UIAlertView类旨在按原样使用,不支持子类。这个类的视图层次是私有的,不能修改

+0

感谢您的回复!非常详细和有用! – Richard

相关问题