简体   繁体   中英

super view - view - sub view. Implementation that masks textField not interactable. iOS. Help please

Thank you for looking, I try to implement in the super view (view controller) a custom view I created.

fromAge = [[simpleFormCell alloc]initWithFrame:CGRectMake(0, spaceFromTopLabel + 190, 200, 30)];
fromAge.title = @"Age";
fromAge.placeholder = @"";
fromAge.widthOfTextField = 60; 
[fromAge populateView];
fromAge.textField.delegate = self;
fromAge.textField.tag = fromAgeTextFieldTag;
[fromAge.textField setInputView:multipurposePicker];
[scrollViewBackground addSubview:fromAge];

toAge = [[simpleFormCell alloc]initWithFrame:CGRectMake(0, spaceFromTopLabel + 190, 200, 30)];
toAge.title = @"to";
toAge.placeholder = @"";
toAge.widthOfTextField = 60; 
toAge.leftPaddingForLabel = 210;
toAge.leftPaddingForTextField = 240;
[toAge populateView];
toAge.textField.delegate = self;
toAge.textField.tag = toAgeTextFieldTag;
[toAge.textField setInputView:multipurposePicker];
[scrollViewBackground addSubview:toAge];

simpleFormCell is a custom view with the following implementation:

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    title = @"test";
    placeholder = @"test2";
    numberofLine = 1;
    leftPaddingForLabel = 20;
    leftPaddingForTextField = 140;
    widthOfLabel = 90;
    widthOfTextField = 160;
    height = 30;
    //[self populateView];
}
return self;
}

- (void)populateView{
label = [[UILabel alloc]initWithFrame:CGRectMake(leftPaddingForLabel, 0, widthOfLabel, (height * numberofLine))];
label.text = title;
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[self addSubview:label];

textField = [[UITextField alloc]initWithFrame:CGRectMake(leftPaddingForTextField, 0, widthOfTextField, (height * numberofLine))];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = placeholder;
textField.textAlignment = UITextAlignmentCenter;
textField.tag = tag;
[textField setReturnKeyType:UIReturnKeyNext];
[self addSubview:textField];
}

I have a similar method that called [self populateView] in the init. That seems to work fine. Please assume that multipurposePicker is working correctly. This is the code but wont apply to these two instances.

- (id)initWithPreset1:(CGRect)frame title:(NSString *)titleValue numberOfLine:(int)numberOfLineValue delegate:(id) delegateValue  tag:(int)tagValue{
self = [super initWithFrame:frame];
if (self) {
    title = titleValue;
    placeholder = @"";
    numberofLine = numberOfLineValue;
    leftPaddingForLabel = 20;
    leftPaddingForTextField = 140;
    widthOfLabel = 110;
    widthOfTextField = 160;
    height = 30;
    tag = tagValue;
    [self populateView];
    textField.delegate = delegateValue;
}
return self;
}

These white boxes wont respond to my touch. (textFieldDidBeginEditing wont fire).

在此处输入图片说明

[fromAge becomeFirstResponder] works. It just wont respond to my touch. I made sure that there is nothing on top of it. Please help. Thank you!

I found the problem! Anna Karenina's comment guided me to the right solution. This has nothing to do with view hierarchy as I first thought. What happened was toAge was blocking fromAge. This makes fromAge textfield non-responsive. As per Anna's comment, eventhough toAge textfield was shown, it was out of the view that supports it. This prevents it from accepting any touches. Thus neither boxes receive any touches. Thank you Anna!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM