代码之家  ›  专栏  ›  技术社区  ›  Olie

如何以编程方式将触摸事件假设为UIButton?

  •  173
  • Olie  · 技术社区  · 15 年前

    UI 尽可能用链条。所以,我想做的是以编程方式触发按钮按下,就像用户在 GUI .

    能够 打电话给 IBAction 选择器,但是,同样,这个特定应用程序的性质使得我伪造实际的按钮按下很重要,这样 IBAction公司 从按钮本身调用。)

    做这件事的首选方法是什么?

    6 回复  |  直到 7 年前
        1
  •  455
  •   Rogare    9 年前

    结果发现

    [buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];
    

    在这种情况下,我得到了我所需要的。

    编辑:


    对于Swift 3:

    buttonObj.sendActions(for: .touchUpInside)

        2
  •  50
  •   Hellojeffy    9 年前

    Swift的最新答案

    buttonObj.sendActionsForControlEvents(.TouchUpInside)
    

    编辑 :为Swift 3更新

    buttonObj.sendActions(for: .touchUpInside)
    
        3
  •  6
  •   Jeff Kelley    15 年前

        4
  •  5
  •   Hemang    12 年前

    在这种情况下, UIButton 是从 UIControl . 这适用于派生自 .

    UIBarButtonItem “对特定用例的操作。在这里, 栏按钮项 sendActionsForControlEvents:

    但幸运的是, 栏按钮项

     if(notHappy){        
             SEL exit = self.navigationItem.rightBarButtonItem.action;
             id  world = self.navigationItem.rightBarButtonItem.target;
             [world performSelector:exit];
     }
    

    在这里, rightBarButtonItem 属于类型 栏按钮项 .

        5
  •  5
  •   Community Mohan Dere    9 年前

    用于Xamarin iOS

    btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);
    

    Reference

        6
  •  4
  •   Gobi M    9 年前

    银行代码3:

    self.btn.sendActions(for: .touchUpInside)
    
        7
  •  -1
  •   elemento    6 年前

    自我。你的按钮(自我)