tvos - Programatically addTarget action to UIButton -
i tested ton of samples none of them works me. here code:
override func viewdidload() { super.viewdidload() let button = uibutton(frame: cgrect(x: 100, y: 100, width: 200, height: 50)) button.backgroundcolor = .green button.settitle("test button", for: .normal) button.titlelabel?.sizetofit() button.addtarget(self, action: #selector(viewcontroller.buttontapped(_:)), for: .touchupinside) self.view.addsubview(button) } func buttontapped(_ sender: uibutton) { print("button tapped.") }
what missing?
well, following this answer issue .touchupinside
. in tvos should .primaryactiontriggered
.
button.addtarget(self, action: #selector(viewcontroller.buttontapped(_:)), for: .primaryactiontriggered)
Comments
Post a Comment