UITableViewCell 的 UITextField 子视图成为第一响应者?

发布于 2024-08-29 17:43:06 字数 904 浏览 4 评论 0原文

我有一个核心数据应用程序,它使用导航控制器深入到详细视图,然后如果您编辑详细视图中的一行数据,您将进入该单行的编辑视图,就像在苹果的 CoreDataBooks 中一样示例(除了 CoreDataBooks 只使用它自己的 UITextField ,而不是像我这样的 UITableViewCell 的子视图)!

编辑视图是一个 UITableviewController,它以编程方式在单元格中创建具有单节单行和 UITextfield 的表格。

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

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

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

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

无论如何,有没有办法在不创建笔尖的情况下解决这个问题,我想保持实现程序化,以实现更大的灵活性。

非常感谢

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)!

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

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];
}

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.

Many Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琉璃繁缕 2024-09-05 17:43:06

在与 Apple 开发支持团队交谈后,我得到了答案!

您需要做的是在 -(void)loadView; 中创建一个离屏 UITextField ,然后将其设置为第一响应者,然后在 viewDidLoad 上方法中,您可以将 UITableViewCell 中的 UITextField 设置为第一响应者。这是一些示例代码(请记住,我是在 UITableViewController 中执行此操作,因此我也创建了表视图!

- (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];
}

我希望这可以帮助任何与我处于同一位置的人!

如果其他人可以看到更好的请说出这样做的方法。

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

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.

何时共饮酒 2024-09-05 17:43:06

尝试在 viewDidAppear 方法中执行此操作,对我有用。

Try do it in viewDidAppear method, works for me.

倾听心声的旋律 2024-09-05 17:43:06

我认为显而易见的解决方案是在视图控制器的 init 方法中创建文本字段。这通常是您配置视图的地方,因为视图控制器确实需要填充的视图属性。

然后,您可以将文本字段设置为 viewWillAppear 中的第一响应者,并且当视图滑入时键盘应该可见。

I think the obvious solution is to create the textfield in the init method of the view controller. That is usually where you configure the view because a view controller does require a populated view property.

Then you can set the textfield as first responder in viewWillAppear and the keyboard should be visible as the view slides in.

春风十里 2024-09-05 17:43:06

您是否尝试过使用 uinavigationcontroller 委托方法?:

navigationController:willShowViewController:animated:

have you tried using the uinavigationcontroller delegate methods?:

navigationController:willShowViewController:animated:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文