Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何使用不同按钮在一个iAction内使用多个UIAlertView?_Iphone_Objective C_Uialertview_Ibaction - Fatal编程技术网

Iphone 如何使用不同按钮在一个iAction内使用多个UIAlertView?

Iphone 如何使用不同按钮在一个iAction内使用多个UIAlertView?,iphone,objective-c,uialertview,ibaction,Iphone,Objective C,Uialertview,Ibaction,是否可以根据不同的条件在同一iAction中使用不同的按钮显示不同的警报??我在IBAction中有一些代码: - (IBAction)btnRecommendationTapped:(id)sender { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" message: @"It se

是否可以根据不同的条件在同一iAction中使用不同的按钮显示不同的警报??我在IBAction中有一些代码:

- (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];
}
我想有另一个警报显示“对不起!我们无法提供任何建议”与确定按钮。当条件满足时,将显示iAction中的条件,否则将显示此条件。

请尝试以下操作:

- (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];

}
}

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

选项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];
}
- (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];
}
第二个选项将非常简单,但我们将执行以下操作,仍然使用
if语句
,但我们只创建一个
UIAlertView

选项2:

- (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];
}
- (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 MySubClass : MySuperClass <UIAlertViewDelegate>
签出和

关于子类化的非常重要的注意事项
UIAlertView
上的苹果文档中,您也最遵守

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


if(条件){create alertView}else{create other alertView}
?!情况如何?你听说过一个
if语句吗
?惯例上说
按钮点击:
应该是
按钮点击:
是额外的
是一个打字错误,如果不是的话,你的答案是错的。谢谢你的回复!非常详细和有用!!