如何获取在tvOS(AppleTV)上运行的UICollectionView上的单元格中的按钮以获得焦点?

Eth*_*len 4 objective-c apple-tv uicollectionview tvos

这是我的基本代码.它在UICollectionView中显示了五个单元格(每个单元格中都有一个简单的按钮).所有按钮都指向单个IBAction方法.这是Main.storyboard中的所有设置.

我不能让tvOS把焦点放在按钮上.知道为什么吗?

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    return cell;
}

- (IBAction)buttonAction
{
    // do stuff
}

@end
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Adn*_*tab 12

默认情况下,UICollectionViewCell是一个可聚焦的手段,它将获得焦点而不是其子视图,但您可以通过覆盖preferredFocusedView并返回您希望获得焦点的视图来覆盖此功能.

- (UIView *)preferredFocusedView
{
    return _button;
}
Run Code Online (Sandbox Code Playgroud)

详细了解初始焦点和首选焦点链