How do you add an action to a button programmatically in xcode

后端 未结 10 1237
野的像风
野的像风 2020-11-29 21:40

I know how to add an IBAction to a button by dragging from the interface builder, but I want to add the action programmatically to save time and to avoid switching back and

10条回答
  •  Happy的楠姐
    2020-11-29 21:56

    Swift answer:

    myButton.addTarget(self, action: "click:", for: .touchUpInside)
    
    func click(sender: UIButton) {
        print("click")
    }
    

    Documentation: https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

提交回复
热议问题