设置表的第一个响应者视图,但我们不知道它的类型(单元格/页眉/页脚) [英] setting the first responder view of the table but we don't know its type (cell/header/footer)
问题描述
我有 UITableViewController 。在这个控制器中,我添加了一个UITextField的子视图UIView。当UITextField得到第一响应者
I have UITableViewController. In this controller I add a subview UIView with UITextField in it. When the UITextField get first responder got
设置表的第一个响应者视图但我们不知道它的
类型(单元格/页眉/页脚)
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直接添加到表视图中。表视图期望将交互式子视图添加到tableHeaderView,tableFooterView或单元格内。
The warning is telling you that you added opaqueView to the table view directly. Table views expect interactive subviews to be added to tableHeaderView, tableFooterView, or within a cell.
违规行为 [self.view addSubview:opaqueView ];
因为self.view是表格视图。
The offending line is [self.view addSubview:opaqueView];
because self.view is the table view.
你可以解决这个问题的几种方法之一。如果您使用的是导航控制器,则可以在其中推送一个带有opaqueView的新控制器。您可以在表视图中弹出一个模态视图,其中包含opaqueView。您可以更改结构,使self.view不是表格视图(这是很多工作)。
You can fix this is one of several ways. If you are using a navigation controller, you can push a new controller with opaqueView in it. You can pop up a modal view over the table view with opaqueView in it. You can change the structure such that self.view is not a table view (which is a lot of work).
更新
UPDATE
这个答案有点忙,所以我会发布更新。我发现Apple使用了与我建议的不同的解决方案。
This answer is kind of busy, so I will post an update. I discovered that Apple uses a different solution then the ones I suggested.
而不是 [self.view addSubview:opaqueView];
尝试将子视图添加到表视图中,您可以执行 [self.view.window addSubview:opaqueView];
将子视图直接添加到表视图中窗口。您需要记住,您正在使用视图,因此边界和框架可能与 self.view
不同。
Instead of [self.view addSubview:opaqueView];
which tries to add the subview to a table view, you can do [self.view.window addSubview:opaqueView];
which adds the subview directly to the table view's window. You need to keep in mind that you are using a view so the bounds and frame may be different than self.view
.
这篇关于设置表的第一个响应者视图,但我们不知道它的类型(单元格/页眉/页脚)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!