[英]UITextField subview of UITableViewCell to become first responder?
我有一个核心数据应用程序,它使用导航控制器深入查看详细信息视图,然后如果您在详细信息视图中编辑其中一行数据,您将进入该单行的编辑视图,就像在Apples CoreDataBooks中一样示例(除了CoreDataBooks本身只使用UITextField
,而不是像我的UITableViewCell
的子视图)!
编辑视图是一个UITableviewController
,它以编程方式在单元格中创建一个单个部分单行和一个UITextfield
表。
我想要发生的是当您选择要编辑的行并将编辑视图推入导航堆栈并且编辑视图在屏幕上移动时,我希望将文本字段选为firstResponder,以便键盘已经显示当视图在屏幕上移动以占据位置。 就像在联系人应用程序或CoreDataBooks应用程序中一样。
我目前在我的应用程序中有以下代码导致视图加载,然后你看到键盘出现(这不是我想要的,我希望键盘已经在那里)
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[theTextField becomeFirstResponder];
}
你不能将它放在-viewWillAppear
因为尚未创建文本字段,所以theTextField
为nil。 在CoreDataBooks应用程序中,他们实现我想要的东西,他们从笔尖加载他们的视图,所以他们使用相同的代码,但在-viewWillAppear
因为文本字段已经创建!
无论如何在没有创建笔尖的情况下解决这个问题,我希望保持实现编程以实现更大的灵活性。
非常感谢
在与Apple Dev支持团队交谈后,我得到了答案!
你需要做的是在-(void)loadView;
创建一个offscreen UITextField
-(void)loadView;
然后将其设置为第一响应者,然后在viewDidLoad
方法上,您可以将UITableViewCell
的UITextField
设置为第一响应者。 下面是一些示例代码(记住我在UITableViewController
这样做,所以我也创建了tableview!
- (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];
}
我希望这有助于任何与我保持同一位置的人!
如果其他人能够看到更好的方法来做到这一点,请说。
尝试在viewDidAppear方法中执行此操作,适合我。
我认为显而易见的解决方案是在视图控制器的init
方法中创建文本字段。 这通常是配置视图的位置,因为视图控制器确实需要填充的视图属性。
然后,您可以在viewWillAppear
中将文本字段设置为第一响应者,并且在视图滑入时键盘应该是可见的。
你尝试过使用uinavigationcontroller委托方法吗?:
navigationController:willShowViewController:动画:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.