我有一个UIView
(MyView)的子类有一些UITextField
作为子视图。 MyView实现UITextFieldDelegate
协议,以便在单击文本字段时收到通知。这很好。现在,我需要将文本框放在一个“容器”中,以便能够使用UIView
动画淡入淡出此容器(及其所有子项)。所以我创建了一个UIView(MySubview),使其成为MyView的子视图,并将所有文本字段放入其中。动画效果很好,但UITextFieldDelegate
不再被调用。我认为这是因为textfields不再是MyView的直接子代。 有没有其他方法可以解决这个问题?嵌套的UITextFields不被调用的UITextFieldDelegate
UPDATE
我做了一个小版本的我的代码,也许这有助于发现问题:
@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;
}
你能编辑文本字段吗? – EmptyStack
嗨桑德拉,我假设一切都与这写在相同的控制器?即MySubView不是一个单独的类,而只是在同一个控制器中的UITextFields及其委托方法被写入的变量。当然,我确定你会完成UITextFields上的委托任务。 – Madhu
将代表写入单独的类文件中,并使用tat类名称代替在文本字段的委托属性中提供自我 – booleanBoy