5

1 つの View Controller に 2 つのアラートを設定するというスマイル タスクを実行しようとしています。以下のコードは問題なく動作しますが、View Controller の別の場所に別のインスタンスを作成するにはどうすればよいでしょうか。コードを複製すると、buttonIndex がどのアラートに応答しているかがわからないのではないかと心配しています。何か案は?ありがとう!

-(void)alertChoice
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) 
    {
    //do something
    }
}
4

4 に答える 4

25

tagプロパティ onを使用して、UIAlertViewどのアラートがどれであるかを解読できます。

-(void)alertChoice
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    alert.tag = 0;
    [alert show];
}

-(void)alertChoice1
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    alert1.tag = 1;
    [alert1 show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 0)
    {
    }
}
于 2013-01-31T09:04:42.470 に答える
2

各 Alert ビューにタグを設定し、どれがメッセージを送信したかを確認するだけです。

alertView.tag=0;

その後

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if(alertView.tag==0){

      if(buttonIndex == 0)//OK button pressed
     {
       //do something
     }
     else if(buttonIndex == 1)//Annul button pressed.
     {
      //do something
     }
}else{

  if(buttonIndex == 0)//OK button pressed

       {
        //do something
      }
      else if(buttonIndex == 1)//Annul button pressed.
    {
    //do something
    }
  }
于 2013-01-31T09:06:46.227 に答える
2

タグを警告ビューに設定します。

alert.tag = 1;

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 && alertView.tag == 1) 
    {
    //do something
    }
}
于 2013-01-31T09:04:44.737 に答える
1

ステップ 1: View Controller.h ファイルにUIAlertViewDelegateを追加する

ステップ 2: ビュー Controller.m ファイルに次のメソッドを追加します。

-(void)AlertMethodOne
{
  UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"AlertMethodOne" message:@"AlertMethodOne successfully Called" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    alert.tag=101;
    [alertview show];  
}

-(void)AletrMethodTwo
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"AletrMethodTwo" message:@"AlertMethodTwo successfully Called" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
alert.tag=102;
    [alertview show]; 
}

以下のように、viewController で上記の 2 つのメソッドを呼び出します。[自己 AlertMethodTwo];

Now AlertView Button Clicked メソッド

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==101)
{
   if (buttonIndex == 0)
   {
   }
}
if(alertView.tag==102)
{
   if (buttonIndex == 1)
   {  
   }
}
}
于 2013-03-30T10:09:27.867 に答える