多按钮、多视图的UIalertview相同



在我的应用程序中,我有4个UI按钮。点击每个按钮显示相同的警报视图包含学习和播放选项,点击警报视图时,要显示不同的视图取决于UI按钮的选择。这是我的警报视图代码。请帮助我执行不同的视图点击选项。

-(IBAction)animals
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];     
[alert1 show];
}
-(IBAction)birds
{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert2 show];    
}
-(IBAction)direct
{ 
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert3 show];
}
-(IBAction)fruit
{
UIAlertView *alert4=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert4 show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"LEARN"]) 
{

animallearn *aview=[[animallearn alloc]initWithNibName:@"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
}
else if([buttonTitle isEqualToString:@"PLAY"])
{ 
birdslearn *bview=[[birdslearn alloc]initWithNibName:@"birdslearn" bundle:nil];
[self.navigationController pushViewController:bview animated:YES];
}
else
{
return;
}
}
-(IBAction)animals
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];     
alert1.tag = 1;  
[alert1 show];
}
-(IBAction)birds
{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
alert2.tag = 2;  
[alert2 show];    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if(alertView.tag == 1)
{
//animal alert
}
else if(alertView.tag == 2)
{
//birds alert
}
// and so on......
}

UIAlertView有一个标记属性。这是一个int。你应该给你的标签分配不同的值。

-(IBAction)direct
{ 
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert3 setTag:3];
[alert3 show];
}

然后单击ButtonAtIndex代理方法,您应该观察当前警报视图的标签:

if(alertView.tag == 3) //etc ...

我不知道你是否在使用ARC,但如果没有,你必须在实例化警报视图时添加一个自动释放程序。

全局声明您的UIAlertView,然后您可以将它们与UIAlertView委托对象进行比较,如下所示-

UIAlertView *alert1;
UIAlertView *alert2;
-(IBAction)animals
{
alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];     
[alert1 show];
}
-(IBAction)birds
{
alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
[alert2 show];    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView ==alert1)
{
if (buttonIndex==1)
{
}
if(buttonIndex == 2)
{
}
}    
if(alertView == alert2)
{
if (buttonIndex==1) 
{

}
}

根据这一点,你将不需要标签:)只需使用条件

-(IBAction)animals
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert1 show];
[alert1 setTag:101];
}
-(IBAction)birds
{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert2 show];
[alert2 setTag:102];
}

-(IBAction)direct
{
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert3 show];
[alert3 setTag:103];
}

-(IBAction)fruit
{
UIAlertView *alert4=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];
[alert4 show];
[alert4 setTag:104];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// learn has indextag = 0 ,play =1 cancel = 2
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 101) {

switch (buttonIndex) {
case 0:
animallearn *aview=[[animallearn alloc]initWithNibName:@"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
break;
case 1:
birdslearn *bview=[[birdslearn alloc]initWithNibName:@"birdslearn" bundle:nil];
[self.navigationController pushViewController:bview animated:YES];
break;
case 2:
return;
break;

default:
break;
}
}
if (alertView.tag == 102) {

switch (buttonIndex) {
case 0:
animallearn *aview=[[animallearn alloc]initWithNibName:@"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
break;
case 1:
birdslearn *bview=[[birdslearn alloc]initWithNibName:@"birdslearn" bundle:nil];
[self.navigationController pushViewController:bview animated:YES];
break;
case 2:
return;
break;

default:
break;
}
}
// perform the same for tag = 103 and 104
}

最新更新