设置表的第一个响应者视图,但我们不知道它的types(单元格/页眉/页脚)

我有UITableViewController 。 在这个控制器中,我添加了UITextField的子视图UIView。 当UITextField得到第一响应者

设置表的第一个响应者视图,但我们不知道它的types(单元格/页眉/页脚)

代码如下:

@interface UserAlbumListViewController (){ NSString *newAlbumName; UITextField *albumNameField; } @end -(void)addAlbumButtonPressed:(id)sender{ self.tableView.scrollEnabled = NO; CGRect frame = self.view.frame; UIView *opaqueView = [[UIView alloc] initWithFrame:frame]; opaqueView.tag = 1001; opaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; UIView *controllerView = [[UIView alloc] init]; controllerView.tag = 1002; controllerView.backgroundColor = [UIColor whiteColor]; controllerView.frame = CGRectMake(10.0f, 60.0f, frame.size.width - 20.0f, 90.0f); [opaqueView addSubview:controllerView]; albumNameField = [[UITextField alloc] init]; albumNameField.borderStyle = UITextBorderStyleRoundedRect; albumNameField.placeholder = NSLocalizedString(@"Album Name",nil); albumNameField.keyboardType = UIKeyboardTypeDefault; albumNameField.returnKeyType = UIReturnKeyDefault; albumNameField.autocorrectionType = UITextAutocorrectionTypeNo; albumNameField.autocapitalizationType = UITextAutocapitalizationTypeNone; albumNameField.clearButtonMode = UITextFieldViewModeWhileEditing; albumNameField.clearsOnBeginEditing = NO; albumNameField.text = @""; albumNameField.frame = CGRectMake(10.0f , 10.0f, controllerView.frame.size.width - 20.0f, 30.0f); [albumNameField becomeFirstResponder]; [controllerView addSubview:albumNameField]; UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _cancelButton.backgroundColor = [UIColor clearColor]; [_cancelButton setTitle:NSLocalizedString(@"Cancel",nil) forState:UIControlStateNormal]; _cancelButton.frame = CGRectMake( controllerView.frame.size.width - 90.0f, albumNameField.frame.origin.y + albumNameField.frame.size.height + 10.0f, 80.0f, 30.0f); [_cancelButton addTarget:self action:@selector(opaqueViewCancelButtonClicked:) forControlEvents:UIControlEventTouchDown]; [controllerView addSubview:_cancelButton]; UIButton *_OKButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _OKButton.backgroundColor = [UIColor clearColor]; [_OKButton setTitle:NSLocalizedString(@"OK",nil) forState:UIControlStateNormal]; _OKButton.frame = CGRectMake( _cancelButton.frame.origin.x - 90.f, _cancelButton.frame.origin.y, 80.0f, 30.0f); [_OKButton addTarget:self action:@selector(opaqueViewOKButtonClicked:) forControlEvents:UIControlEventTouchDown]; [controllerView addSubview:_OKButton]; [self.view addSubview:opaqueView]; [controllerView release]; [opaqueView release]; } 

如何避免这个警告?

警告告诉你,你直接添加opaqueView到表视图。 表视图期望交互式子视图被添加到tableHeaderView,tableFooterView或单元格内。

有问题的一行是[self.view addSubview:opaqueView]; 因为self.view是表视图。

你可以修复这是几种方法之一。 如果您正在使用导航控制器,则可以使用opaqueView来推送一个新的控制器。 你可以用opaqueView在表格视图上popup一个模态视图。 你可以改变结构,使self.view不是一个表视图(这是很多工作)。


UPDATE

这个答案很忙,所以我会发布一个更新。 我发现苹果公司使用了不同的解决scheme,然后我build议。

而不是[self.view addSubview:opaqueView]; 它试图添加子视图到一个表视图,你可以做[self.view.window addSubview:opaqueView]; 它将子视图直接添加到表视图的窗口。 你需要记住,你正在使用一个视图,所以边界和框架可能不同于self.view

当推送到NavigationController的视图是UITableView的一个实例而不是UIView的时候,我得到了这个“(cell / header / footer)”错误。 然后在这个类的Xib编辑器中,我在canvas上添加了一个视图,然后文本最终通过更多的视图添加到视图中。 UI Builder将允许您指定UITableView作为视图的超类,并在canvas上放置一个视图,即使向该视图添加项目,也会导致消息“设置表的第一个响应者视图,但我们不知道它的types(细胞/页眉/页脚)”。