简体   繁体   English

UITableView单元格中的UITextField以编程方式成为FirstFirstResponder

[英]UITextField in UITableView cell becomeFirstResponder programmatically

I have a UITextField with a tag inside a prototype cell. 我有一个UITextField ,在原型单元格中有一个标签。 The UITextField is set to become first responder when the UITableView is being built and a UISwitch in the cell above is turned on. 在构建UITableView并打开上面单元格中的UISwitch时, UITextField将设置为第一响应者。 This works if the app starts from scratch or if it was closed and restarted. 如果应用程序从头开始或者已关闭并重新启动,则此方法有效。 Once it's loaded the [tableView reloadData] doesn't trigger the becomeFirstResponder anymore. 一旦加载, [tableView reloadData]就不再触发becomeFirstResponder了。

If the UITextField becomes first responder by touching the textfield later on, I can trigger the becomeFirstResponder event with buttons, with pop ups,... But not with my switch any more. 如果UITextField稍后通过触摸文本字段成为第一响应者,我可以使用按钮触发becomeFirstResponder事件,弹出窗口,...但不再使用我的开关。

Any pointers as to what I can try? 关于我可以尝试的任何指示?

Currently, I use the didSelectRowAtIndexPath method to trigger a pop up. 目前,我使用didSelectRowAtIndexPath方法来触发弹出窗口。 A nice side effect is, that when I pull up the number keypad, I can provide an ok and cancel button within the pop up instead of having to fiddle with separate buttons on the keypad. 一个很好的副作用是,当我拉起数字键盘时,我可以在弹出窗口中提供一个确定和取消按钮,而不必在键盘上摆弄单独的按钮。 But it just seems so obviously to be a workaround. 但似乎很明显是一个解决方法。

This is how I call firstresponder when building the UITableView (which works every time the app starts from scratch): 这就是我在构建UITableView时调用firstresponder的方法(每次应用程序从头开始时都会起作用):

if ([settings doubleForKey:@"limitDouble"]==0 && [settings boolForKey:@"limitBool"]==YES) {
    [dailyLimitEntry becomeFirstResponder];
}

dailyLimitEntry is a UITextField which is strong so it stays around. dailyLimitEntry是一个强大的UITextField ,所以它保持不变。

Just for fun I added a button and connected it to my code like this: 为了好玩,我添加了一个按钮并将其连接到我的代码,如下所示:

UITextField *tmp = (UITextField *)[self.view viewWithTag:35];
[tmp becomeFirstResponder];

This works, too. 这也有效。 When I use it with the switch, it's only called once the app is freshly loaded in the memory. 当我将它与交换机一起使用时,只有在应用程序新加载到内存中时才会调用它。 Otherwise, my UITextField doesn't respond to the switch. 否则,我的UITextField不响应交换机。

After the first comments, I found a method to check whether or not the UITableView has finished reloading 在第一次评论之后,我找到了一种检查UITableView是否已完成重新加载的方法

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] == ((NSIndexPath*)[[settingsTableView indexPathsForVisibleRows] lastObject]).row){
        //end of loading
        //for example [activityIndicator stopAnimating];
        NSLog(@"finished reload");
        NSLog(@"%@",dailyLimitEntry);
        if ([settings boolForKey:@"limitBool"]==YES) {
            UITextField *tmp = (UITextField *)[self.view viewWithTag:35];
            [tmp becomeFirstResponder];
        }
    }
}

Fun thing though is, become first responder is only triggered the first time the switch is used after the app loaded. 有趣的是,成为第一响应者只在应用程序加载后第一次使用开关时触发。 Initially, the switch is off. 最初,开关关闭。 So the cell containing the UITextField is not drawn yet. 因此,尚未绘制包含UITextField的单元格。 Once I switch to ON , the UITextField gets drawn in the second cell and becomes first responder. 一旦我切换到ONUITextField将被绘制在第二个单元格中并成为第一个响应者。 If I switch OFF and ON again, I still get my log "finished reload", but the UITextField doesn't become first responder. 如果我再次OFFON ,我仍然会将我的日志“完成重新加载”,但是UITextField不会成为第一响应者。 It's driving me nuts.... Must be something about the life cycle of the app. 它让我疯狂......必须要关注应用程序的生命周期。

Rather than checking the indexPath, check the cell's class. 而不是检查indexPath,检查单元格的类。

Here's what I do to bring up the keyboard for a title cell that's blank (when I create a new item): 这是我为标题单元格(当我创建新项目时)调出键盘所做的操作:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell isKindOfClass:[FDSSampleTitleCell class]]) {
        FDSSampleTitleCell *titleCell = (FDSSampleTitleCell *)cell;
        if (titleCell.titleField.text.length == 0) {
            [titleCell.titleField becomeFirstResponder];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM