【问题标题】:How to pass UIAction without touching the UIButton如何在不触摸 UIButton 的情况下传递 UIAction
【发布时间】:2013-06-02 01:12:51
【问题描述】:

我有一个用作开关的 UIButton。 当用户点击它时,它的状态变为“选中”并调用动作“一”。当用户再次点击时,UIButton 状态变为“未选中”并且该操作不再可用。

有没有办法通过点击一个完全不同的 UIButton 来将 UIButton 设置为“已选择”并将其更改为“已选择”并调用相同的操作?

干杯

【问题讨论】:

  • 请对您的问题进行清楚的描述
  • 你的意思是你想调用相同的方法到两个不同的按钮点击。对吗?
  • 所以你想像这里一样伪造触摸事件:how to programmatically fake a touch event to a UIButton?
  • 谢谢阿迪尔。这就是我一直在寻找的东西。为我做了诀窍。

标签: iphone uibutton ibaction


【解决方案1】:

这样做:

-(IBAction)switchButtonClicked:(UIButton*)sender
{

    if(sender.isSelected)
      {
        .....
      }
    else
      {
        ...
      }
    [sender setSelected:!sender.isSelected];//This will work as Switch.
}

XIB 或从代码中设置UIButtonSelected(Switch ON)default(Switch OFF) 属性(图像和标题)。 只需一个按钮,您就可以使用此代码实现功能

【讨论】:

    【解决方案2】:

    请为该按钮操作编写一个函数,例如,

    -(void)Buttonaction
    
    {
    
    do your action here
    
      also set your button has selected state.
    
      buttonname.setselected=yes;
    
    }
    

    在您按下按钮时调用此函数。

    【讨论】:

      【解决方案3】:

      根据我从问题中了解到的情况,我想给你我的建议

      if (!<your_button>.selected)
      {
          <your_button>.selected = YES;
          // do your stuff here
      }
      else
      {
          <your_button>.selected = NO;
          // do your stuff here, in your case action should be no longer available so that,
          <your_button>.userInteractionEnabled = NO;
      }
      

      享受编程!

      【讨论】:

        【解决方案4】:

        UIbutton 的状态称为selected,并通过属性selected 进行管理

        所以要让一个按钮进入选中状态

        [buttonInstance setSelected:YES];
        

        要使其未选中,请使用

        [buttonInstance setSelected:NO];
        

        【讨论】:

          【解决方案5】:

          当你点击 uibutton 进行操作时,很简单,在该按钮的操作中,你可以将按钮状态设置为选中,然后在下一行代码中你可以调用该方法(操作)

          - (IBAction)btn1Touched:(id)sender {
              [_btn2 setSelected:YES];
              [self btn2Touched:self];
          }
          - (IBAction)btn2Touched:(id)sender {
          
              NSLog(@"btn Action called");
          }
          

          【讨论】:

            【解决方案6】:

            试试这样,'

             -(IBAction)nextbutton:(id)sender{
                UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
                btn.selected=YES;
                //do whatevr you want.
            
                }
            

            【讨论】:

              猜你喜欢
              • 2018-03-06
              • 2023-03-19
              • 1970-01-01
              • 2012-02-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-11-27
              相关资源
              最近更新 更多