我有一个UIView
(MyView) 的子类,其中有一些UITextField
子视图。MyView 实现了该UITextFieldDelegate
协议,以便在单击文本字段时得到通知。这运作良好。现在我需要将文本字段放在一种“容器”中,以便能够通过UIView
动画淡入和淡出这个容器(及其所有子项)。所以我创建了一个 UIView (MySubview),使它成为 MyView 的子视图,并将所有文本字段放在其中。动画效果很好,但UITextFieldDelegate
不再被调用。我认为这是因为文本字段不再是 MyView 的直接子项。有没有其他方法可以解决这个问题?
更新
我做了一个小版本的代码,也许这有助于找到问题:
@interface MyView : UIView <UITextFieldDelegate>
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// This is MySubview:
UIView *tempLabelsContainer = [[UIView alloc] initWithFrame:self.bounds];
[tempLabelsContainer setUserInteractionEnabled:YES];
[self addSubview:tempLabelsContainer];
self.labelsContainer = tempLabelsContainer;
[tempLabelsContainer release];
UITextField *aTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
[aTextField setBackgroundColor:[UIColor clearColor]];
[aTextField setText:@"Some text"];
[aTextField setTag:1];
[aTextField setDelegate:self];
[self.labelsContainer addSubview:aTextField];
[aTextField release];
// More labels are being added
}
return self;
}
#pragma mark - UITextFieldDelegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// This is not being called
NSLog(@"TextField with the tag: %d should be edited", [textField tag]);
return NO;
}