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

后端 未结 10 1232
野的像风
野的像风 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条回答
  •  遥遥无期
    2020-11-29 22:05

    For Swift 3

    Create a function for button action first and then add the function to your button target

    func buttonAction(sender: UIButton!) {
        print("Button tapped")
    }
    
    button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside)
    

提交回复
热议问题