是否可以根据不同的条件在同一IBAction中显示不同按钮的不同警报?我在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];
}
我想通过确定按钮显示另一个警告“抱歉!我们无法提供任何建议”。当条件满足时,IBAction中的那个显示另一个。
答案 0 :(得分: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 :(得分: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 Documentation和UIAlertViewDelegate
Apple Documentation
非常重要关于您最常遵守的UIAlertView
上Apple文档中的子类化的注意事项。
UIAlertView类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改
答案 2 :(得分: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];
}
}