swift - Swift:集合视图单元中 UIControl 的 tvOS IBAction 永远不会被调用
问题描述
我知道这已经被问过很多次了,但我仍在学习,我已经在 SO 中尝试了所有可能的解决方案,但我一次都没有运气。所以这是问题:
- 我正在使用 Xcode 11 beta 3(希望这不是问题!)
- 我在视图控制器中有一个简单的集合视图
- 在单元格内,我放置了一个 tvOS 的TVPosterView - 它继承自 UIControl 并自行测试它的行为就像一个按钮(IBAction 被调用)。
- 当我推海报时,在收藏视图上有动画
- 海报视图有一个默认图像,我只是更改标题
- 我将 IBAction 从海报视图拖到 IB 中的单元格类
- IBAction 分开,集合视图显示和滚动就好了,改变了海报的标题
- 如果我将集合视图委托给视图控制器,collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)会被调用。
这是代码:
视图控制器
import UIKit
import TVUIKit
class SecondViewController: UIViewController {
@IBOutlet weak var myView: UIView!
@IBOutlet weak var myCollection: MyCollection!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
extension SecondViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
cell.myPoster.title! = "Header " + String(indexPath.row + 1)
cell.myPoster.tag = indexPath.row + 1
cell.posterTapAction = { cell in
print("Header is: \(cell.myPoster.title!)")
}
return cell
}
}
extension SecondViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// This one works - but that's not what I am looking for
print("From didSelectItemAt indexPath: \(indexPath.item + 1)")
}
}
集合视图
import UIKit
class MyCollection: UICollectionView {
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
细胞
import UIKit
import TVUIKit
class MyCell: UICollectionViewCell {
var posterTapAction: ((MyCell) -> Void)?
@IBOutlet weak var myPoster: TVPosterView!
@IBAction func myAction(_ sender: TVPosterView) {
posterTapAction?(self)
print("Poster pressed \(sender.tag)")
}
}
关于我缺少什么的任何想法?我很乐意在按下海报后设法打印一个虚拟字符串。
我也尝试过使用选择器和委托的解决方案,但都没有奏效。所以,请让我们专注于这个带有闭包的特殊解决方案。谢谢!
解决方案
我终于找到了。像往常一样,这是由于 tvOS 的焦点。基本上,单元格必须不可聚焦,以便单元格内的 UIControl 元素(在我的情况下为 TVPosterView,但它可以是任何 UIControl)将成为焦点。这是骗人的,因为从图形上看,它确实看起来好像海报有焦点(可以将海报旋转一点)。
解决方案是将collectionView(_:canFocusItemAt:)添加到UICollectionViewDelegate
extension SecondViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView,
canFocusItemAt indexPath: IndexPath) -> Bool {
return false
}
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
// Now, thanks to the function above, this is disabled
print("From didSelectItemAt indexPath: \(indexPath.item + 1)")
}
}
我已经在这个问题的代码中对其进行了测试,并将它也添加到了我的项目中,它终于可以工作了!
推荐阅读
- php - 无法在同一页面表单提交上获取 PHP 请求
- android - 如何为 SearchView 投射 CustomEditText 而不是 EditText?
- javascript - 尝试使用 JS 对象填充组件但得到“函数作为 React 子无效”?
- asynchronous - 如何使用任务完成源
- c++ - 加载大 OBJ 文件会导致模型损坏?
- r - 机器特定的慢 read_excel 读取时间
- python - 特征提取 NLP
- php - 两次调用函数时未附加Ajax成功数据
- python - 连接字节时出现类型错误
- python - 如何使用 OpenCV 或深度学习从平面图中扫描房间轮廓数据?