我有一个UIView,上面有两个按钮。在MyView
类中,我有以下代码:
-(BOOL) canBecomeFocused {
return YES;
}
-(NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
return @[_editButton, _addButton];
}
-(IBAction) editTapped:(id) sender {
BOOL editing = !tableViewController.editing;
[_editButton setTitle:editing ? @"Done" : @"Edit" forState:UIControlStateNormal];
_addButton.hidden = !editing;
[tableViewController setEditing:editing animated:YES];
基本思路是用户可以将焦点移动到编辑按钮,然后可以使添加按钮出现。
问题的起因是每次我点击编辑按钮时,焦点会转移到表格视图。实际上,我希望它移动到添加按钮。我还希望在编辑被禁用时,编辑按钮保持焦点。但是,它又向下移动到了表格视图。
所以我尝试了上面的代码。这个方法有效,因为焦点可以移动到视图上并移动到按钮上。但一旦到达那里,我就无法让它移动到其他地方。
我读过的所有内容都说只需覆盖preferredFocusEnvironments
即可,但迄今为止我还没有能够让它起作用。焦点始终会转移到一个按钮,然后拒绝移动到其他任何地方。
有什么想法吗?
UIFocusGuide
,但似乎从按钮到表视图的焦点转移是系统生成的移动,并忽略了我的焦点指南。即使我把它放在它们之间。 Grrr. - drekka