UITextField UITableViewCell子视图成为第一响应者? [英] UITextField subview of UITableViewCell to become first responder?

查看:145
本文介绍了UITextField UITableViewCell子视图成为第一响应者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据应用程序,它使用导航控制器深入查看详细视图,然后如果您在详细视图中编辑其中一行数据,您将进入该单行的编辑视图,如在Apples CoreDataBooks示例中(除了CoreDataBooks本身仅使用 UITextField ,而不是 UITableViewCell 的子视图之类的我的

I have a core data application which uses a navigation controller to drill down to a detail view and then if you edit one of the rows of data in the detail view you get taken to an Edit View for the that single line, like in Apples CoreDataBooks example (except CoreDataBooks only uses a UITextField on its own, not one which is a subview of UITableViewCell like mine)!

编辑视图是一个 UITableviewController ,它创建了一个单行的单行和一个<以编程方式在单元格中code> UITextfield 。

The edit view is a UITableviewController which creates its table with a single section single row and a UITextfield in the cell, programatically.

我想要发生的是当你选择要编辑的行和编辑时视图被推到导航堆栈上,编辑视图在屏幕上移动动画,我希望将文本字段选为firstResponder,以便当视图在屏幕上移动到位置时键盘已经显示。就像在联系人应用程序或CoreDataBooks应用程序中一样。

What I want to happen is when you select a row to edit and the edit view is pushed onto the nav stack and the edit view is animated moving across the screen, I want the textfield to be selected as firstResponder so that the keyboard is already showing as the view moves across the screen to take position. Like in the Contacts app or in the CoreDataBooks App.

我目前在我的应用程序中有以下代码导致视图加载,然后您看到键盘出现(不是我想要的,我希望键盘已经存在)

I currently have the following code in my app which causes the view to load and then you see the keyboard appear (which isn't what I want, I want the keyboard to already be there)

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [theTextField becomeFirstResponder];
}

你不能把它放在 -viewWillAppear 由于尚未创建文本字段,因此 theTextField 为零。在CoreDataBooks应用程序中,他们实现我想要的,他们从笔尖加载他们的视图,所以他们使用相同的代码,但在 -viewWillAppear ,因为已经创建了文本字段!

You can't put this in -viewWillAppear as the textfield hasn't been created yet so theTextField is nil. In the CoreDataBooks App where they achieve what i want they load their view from a nib so they use the same code but in -viewWillAppear as the textfield has already been created!

无论如何在没有创建笔尖的情况下解决这个问题,我希望保持实现编程以实现更大的灵活性。

Is there anyway of getting around this without creating a nib, I want to keep the implementation programatic to enable greater flexibility.

非常感谢

推荐答案

在与Apple Dev支持团队交谈后,我有了答案!

After speaking with the Apple Dev Support Team, I have an answer!

你需要做的是在中创建一个offscreen UITextField - (void)loadView; 然后将其设置为第一响应者然后在 viewDidLoad 方法中,您可以在<$ c中设置 UITextField $ c> UITableViewCell 成为第一响应者。下面是一些示例代码(请记住我在 UITableViewController 中这样做,所以我也创建了tableview!

What you need to do is to create an offscreen UITextField in -(void)loadView; and then set it as first responder then on the viewDidLoad method you can set the UITextField in the UITableViewCell to be first responder. Heres some example code (remember I'm doing this in a UITableViewController so I am creating the tableview as well!

- (void)loadView
{
    [super loadView];

    //Set the view up.
    UIView *theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = theView;
    [theView release];

    //Create an negatively sized or offscreen textfield
    UITextField *hiddenField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, -10, -10)];
    hiddenTextField = hiddenField;
    [self.view addSubview:hiddenTextField];
    [hiddenField release];

    //Create the tableview
    UITableView *theTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
    theTableView.delegate = self;
    theTableView.dataSource = self;
    [self.view addSubview:theTableView];
    [theTableView release];

    //Set the hiddenTextField to become first responder
    [hiddenTextField becomeFirstResponder];

    //Background for a grouped tableview
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //Now the the UITableViewCells UITextField has loaded you can set that as first responder
    [theTextField becomeFirstResponder];
}

我希望这可以帮助任何人和我保持同样的位置!

I hope this helps anyone stuck in the same position as me!

如果其他人可以看到更好的方法,请说。

If anyone else can see a better way to do this please say.

这篇关于UITextField UITableViewCell子视图成为第一响应者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆