我有 UITableViewController。在这个 Controller 中,我添加了一个带有 UITextField 的 subview UIView。当 UITextField 得到第一响应者时
setting the first responder view of the table but we don't know its type (cell/header/footer)
代码如下:
@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 添加到 TableView 。表格 View 期望将交互式 subview 添加到 tableHeaderView、tableFooterView 或单元格内。
违规行是[self.view addSubview:opaqueView];
因为 self.view 是 TableView 。
您可以通过多种方法之一解决此问题。如果您使用的是导航 Controller ,则可以推送一个带有 opaqueView 的新 Controller 。您可以在其中包含 opaqueView 的 TableView 上弹出一个模态视图。您可以更改结构,使 self.view 不是 TableView (这需要大量工作)。
更新
这个回答有点忙,所以我会发布更新。我发现 Apple 使用的解决方案与我建议的解决方案不同。
而不是 [self.view addSubview:opaqueView];
它试图将 subview 添加到 TableView ,你可以做 [self.view.window addSubview:opaqueView];
它将 subview 直接添加到 TableView 的窗口中。您需要记住,您使用的是 View ,因此边界和框架可能与 self.view
不同。 .
关于iphone - 设置表格的第一响应者 View ,但我们不知道它的类型(单元格/页眉/页脚),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10764804/