我通过Swift在tvOS中创建了一个UIButton
let randomBtn = UIButton()
randomBtn.setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal, forState: .Normal)
randomBtn.setImage(RndHoover, forState: .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused)
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth - 150, y: 60 , width: 70 , height: 70)
self.view.addSubview(randomBtn)
但是,如果我按该按钮,操作就永远不会触发,tvOS有什么不同吗?
func click(sender: UIButton) {
print("click")
}
最佳答案
在tvOS中,按钮操作UIControlEvents
TouchUpInside
将不会调用。
您必须像下面那样使用UIControlEvents
PrimaryActionTriggered
。
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
如果您有任何困惑,也请引用此链接
https://forums.developer.apple.com/thread/17925
关于swift - tvOS按钮 Action ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38410885/