0

私のアプリケーションでは、4 つの UIbuttons があります。同じアラート ビューを表示する各ボタンをクリックすると、学習と再生のオプションが含まれます。アラート ビューをクリックすると、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

4 に答える 4

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 を使用しているかどうかはわかりませんが、使用していない場合は、アラート ビューをインスタンス化するときに自動解放を追加する必要があります。

于 2012-10-12T09:43:01.383 に答える
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......
}
于 2012-10-12T09:44:11.027 に答える
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) 
    {


    }
}

これによれば、タグは必要ありません:)単に条件を使用してください

于 2012-10-12T09:46:24.170 に答える
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
}
于 2012-10-12T09:48:56.953 に答える