UITableViewCell 的 UITextField 子视图成为第一响应者?
我有一个核心数据应用程序,它使用导航控制器深入到详细视图,然后如果您编辑详细视图中的一行数据,您将进入该单行的编辑视图,就像在苹果的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在与 Apple 开发支持团队交谈后,我得到了答案!
您需要做的是在
-(void)loadView;
中创建一个离屏UITextField
,然后将其设置为第一响应者,然后在viewDidLoad
上方法中,您可以将UITableViewCell
中的UITextField
设置为第一响应者。这是一些示例代码(请记住,我是在UITableViewController
中执行此操作,因此我也创建了表视图!我希望这可以帮助任何与我处于同一位置的人!
如果其他人可以看到更好的请说出这样做的方法。
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 theviewDidLoad
method you can set theUITextField
in theUITableViewCell
to be first responder. Heres some example code (remember I'm doing this in aUITableViewController
so I am creating the tableview as well!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.
尝试在 viewDidAppear 方法中执行此操作,对我有用。
Try do it in viewDidAppear method, works for me.
我认为显而易见的解决方案是在视图控制器的 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.您是否尝试过使用 uinavigationcontroller 委托方法?:
navigationController:willShowViewController:animated:
have you tried using the uinavigationcontroller delegate methods?:
navigationController:willShowViewController:animated: