用于多按钮,多视图的UIalertview

时间:2012-10-12 09:36:08

标签: iphone ios uialertview

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

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

4 个答案:

答案 0 :(得分:1)

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

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

然后clickedButtonAtIndex委托方法,您应该观察当前警报视图的标记:

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

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

答案 1 :(得分:1)

-(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......
}

答案 2 :(得分:1)

全局声明您的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) 
    {


    }
}

根据这个你不需要标签:)只需使用条件

答案 3 :(得分:0)

-(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
}