swift - tvOS 按钮操作

标签 swift uibutton tvos

我通过 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)

但是如果我按下按钮,这个 Action 永远不会被触发,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 按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410885/

相关文章:

ios - 如何使用 swift 3 在 iOS 上散列文件?

ios - 禁用和启用状态的 UIButton tintColor?

swift - tvOS:有没有办法防止搜索键盘在滚动时折叠?

快速处理从 sleep 中唤醒并恢复播放?

uitabbar - tvOS 的 UITabBarController 中 View Controller 的最大数量是多少

ios - 快速删除表格 View 中的一行

IOS 发布问题(无效的 Swift 支持)

swift - Collection View 中 PNG 图像的透明背景

iphone - 如何在一个方法中捕获并处理 UIButton 的所有事件?

ios - 以编程方式更改 UIButton 的标题在 iOS Xcode-Beta 3 中不起作用