我正在尝试在ViewController中使用TableView作为选择器。
它是一个带有文本标签的简单表视图。
我的问题是,我不能让桌面视图继续下一个文本区域。
我已尝试使用此代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_textfield becomeFirstResponder];
}
但是只打开键盘,在我输入后我回到了tableview。
当我选择一行时,我该怎么做才能离开桌面视图?
答案 0 :(得分:2)
您需要跟踪和更新UITableViewCell
子类UIFocusHeading
,具体取决于是否已选择单元格以及UITableViewCell
是什么。
您应该做的第一件事是在@property (nonatomic, strong) UIView *currentPreferredFocusedSubview;
子类上添加一个属性,以跟踪您想要关注的当前子视图。例如:
didSelectRowAtIndexPath
然后在- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
cell.currentPreferredFocusedSubview = cell.textField;
[cell setNeedsFocusUpdate];
}
的视图控制器中,您应该将其设置为您想要关注的文本字段,然后请求焦点引擎更新:
UITableViewCell
现在,在preferredFocusedView
子类中,您需要返回首选焦点视图,并通过覆盖shouldUpdateFocusInContext
和- (UIView *)preferredFocusedView
{
return self.currentPreferredFocusedSubview;
}
- (BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext *)context
{
if (self.currentPreferredFocusedSubview == self.textField && context.focusHeading == UIFocusHeadingRight)
{
self.currentPreferredFocusedSubview = self.textField2;
}
else if (self.currentPreferredFocusedSubview == self.textField2 && context.focusHeading == UIFocusHeadingRight)
{
self.currentPreferredFocusedSubview = self.textField3;
}
else if (self.currentPreferredFocusedSubview == self.textField3 && context.focusHeading == UIFocusHeadingRight)
{
self.currentPreferredFocusedSubview = self.textField3;
}
else if (self.currentPreferredFocusedSubview == self.textField3 && context.focusHeading == UIFocusHeadingLeft)
{
self.currentPreferredFocusedSubview = self.textField2;
}
else if (self.currentPreferredFocusedSubview == self.textField2 && context.focusHeading == UIFocusHeadingLeft)
{
self.currentPreferredFocusedSubview = self.textField;
}
else if (self.currentPreferredFocusedSubview == self.textField && context.focusHeading == UIFocusHeadingLeft)
{
self.currentPreferredFocusedSubview = self.textField;
}
else
{
self.currentPreferredFocusedSubview = nil;
}
return true;
}
来更改焦点引擎的更新方式:
preferredFocusedView
这里我们只根据焦点引擎的方向更新nil
。如果向上或向下,下一行将变得集中,因为我们正在返回var currentPreferredFocusedSubview: UITextField?
override var preferredFocusedView: UIView? {
get{
return self.currentPreferredFocusedSubview
}
}
override func shouldUpdateFocusInContext(context: UIFocusUpdateContext) -> Bool {
switch (self.currentPreferredFocusedSubview, context.focusHeading) {
case (self.textField?, UIFocusHeading.Right):
self.currentPreferredFocusedSubview = self.textField2
case (self.textField2?, UIFocusHeading.Right):
self.currentPreferredFocusedSubview = self.textField3
case (self.textField3?, UIFocusHeading.Right):
self.currentPreferredFocusedSubview = self.textField3
case (self.textField3?, UIFocusHeading.Left):
self.currentPreferredFocusedSubview = self.textField2
case (self.textField2?, UIFocusHeading.Left):
self.currentPreferredFocusedSubview = self.textField
case (self.textField?, UIFocusHeading.Left):
self.currentPreferredFocusedSubview = self.textField
default:
self.currentPreferredFocusedSubview = nil
}
return true
}
。如果向左或向右,则为相应的文本字段。这有点笨重,但这是以编程方式更新焦点视图的tvOS 9方式。
(export default (withHistory, onUpdate) => {
const history = withHistory?
(Modernizr.history ?
browserHistory
: hashHistory)
: null;
return (
<Router history={history} onUpdate={onUpdate}>
<Route path='/' component={App}>
<IndexRoute component={quotes} />
<Route path='/app/search' component={SimpleSearch} />
<Route path='pathName' component={Blank1} />
<Route path='pathName/:category' component={Blank1}/>
<Route path='pathName/:category(/:page)' component={Blank1}/>
<Route path='app/:id' component={Blank1} />
</Route>
</Router>
);
};