【问题标题】:Two Alert Views in One View Controller - buttonIndex Response一个视图控制器中的两个警报视图 - buttonIndex 响应
【发布时间】:2013-01-31 09:01:15
【问题描述】:

我正在尝试在单个视图控制器中执行具有两个警报的微笑任务。下面的代码工作正常,但我将如何在视图控制器的其他地方创建另一个实例。我担心如果我复制代码,我的 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
    }
}

【问题讨论】:

    标签: ios objective-c uialertview


    【解决方案1】:

    您可以使用UIAlertView 上的tag 属性来破译哪个警报是哪个:

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

    【讨论】:

      【解决方案2】:

      将标签设置为警报视图。

      alert.tag = 1;
      
      - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      {
          if (buttonIndex == 1 && alertView.tag == 1) 
          {
          //do something
          }
      }
      

      【讨论】:

        【解决方案3】:

        只需为每个警报视图设置一个标签,然后检查是哪一个发送了消息。

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

        【讨论】:

          【解决方案4】:

          第 1 步:在视图 controller.h 文件中添加 UIAlertViewDelegate

          第 2 步:在你的 view 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 中调用上述两个方法,如下所示: [自我警报方法一]; [self AlertMethodTwo];

          Now AlertView 按钮点击方法

          -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
          {
          if(alertView.tag==101)
          {
             if (buttonIndex == 0)
             {
             }
          }
          if(alertView.tag==102)
          {
             if (buttonIndex == 1)
             {  
             }
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-27
            • 2020-02-13
            相关资源
            最近更新 更多