简体   繁体   English

UITextfield和UIButton不在UIView内部单击

[英]UITextfield and UIButton not clicking inside UIView

I have a few textfield and button inside a UIView but the click event is not getting called I have checked the Height of the UIView its more than my button and textview. 我在UIView中有一些文本字段和按钮,但是没有调用click事件,我检查了UIView的高度超过了按钮和textview的高度。 As you can see in the screenshot of UIView: Frame of UIView These are the screenshot of TextField and button This is the first Label 如您在UIView的屏幕截图中所见:UIView的框架这些是TextField和按钮的屏幕截图这是第一个Label

And this is the last button 这是最后一个按钮

And in between there are lots of labels and textfield. 在它们之间有很多标签和文本字段。 As you can see in this image This is the image of my view 如您在此图像中看到的, 这是我视图的图像

Also I have set 我也已经设定

userInteractionEnabled=NO 

for the UIView and 对于UIView和

userInteractionEnabled=YES;

for the TextField and Button but still no luck. 用于TextField和Button,但仍然没有运气。 I have no clue what to do next.This is my code 我不知道下一步该怎么办,这是我的代码

- (void)addChemicalTapped:(UIButton *)sender {

    _ans6Constraint.constant = 750;
//    [self.addChemical addTarget:self action:@selector(fixDelaysContentTouches:) forControlEvents:UIControlEventTouchUpInside];


   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:_Ans6Text1new1];

    _chemicalUIview.userInteractionEnabled=NO;
//    _chemical1scroll.userInteractionEnabled=false;
    _Ans6Text4new1.userInteractionEnabled=YES;
    _Ans6Text2new1.userInteractionEnabled=YES;
    _Ans6Text1new1.userInteractionEnabled=YES;
    _Ans6Text3new1.userInteractionEnabled=YES;
    [_RemoveChemical1 setTag:1234];
    [_RemoveChemical1 addTarget:self action:@selector(RemoveChemical1Tapped:) forControlEvents:UIControlEventTouchUpInside];
     [self reloadTable];
}

There is no problem with click event because the id of same textfield and button is getting called in different UIView. 单击事件没有问题,因为同一文本字段和按钮的ID在不同的UIView中被调用。 So there is some thing wrong with my UIView code. 所以我的UIView代码出了点问题。 This is my UIView code 这是我的UIView代码

UIView *paddingView3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
    [_ans6Text1 setClipsToBounds:NO];
    [_ans6Text1.layer setBorderWidth:1.0];
    [_ans6Text1.layer setBorderColor:[UIColor whiteColor].CGColor];
    [_ans6Text1.layer setCornerRadius:4.0];
    [_ans6Text1 setLeftView:paddingView3];
    [_ans6Text1 setLeftViewMode:UITextFieldViewModeAlways];
        UIView *paddingView3new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
        [_Ans6Text1new1 setClipsToBounds:NO];
        [_Ans6Text1new1.layer setBorderWidth:1.0];
        [_Ans6Text1new1.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_Ans6Text1new1.layer setCornerRadius:4.0];
        [_Ans6Text1new1 setLeftView:paddingView3new1];
        [_Ans6Text1new1 setLeftViewMode:UITextFieldViewModeAlways];

    UIView *paddingView4 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
    [_ans6Text2 setClipsToBounds:NO];
    [_ans6Text2.layer setBorderWidth:1.0];
    [_ans6Text2.layer setBorderColor:[UIColor whiteColor].CGColor];
    [_ans6Text2.layer setCornerRadius:4.0];
    [_ans6Text2 setLeftView:paddingView4];
    [_ans6Text2 setLeftViewMode:UITextFieldViewModeAlways];
    [_ans6Text2 setInputView:_datePicker];
        UIView *paddingView4new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
        [_Ans6Text2new1 setClipsToBounds:NO];
        [_Ans6Text2new1.layer setBorderWidth:1.0];
        [_Ans6Text2new1.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_Ans6Text2new1.layer setCornerRadius:4.0];
        [_Ans6Text2new1 setLeftView:paddingView4new1];
        [_Ans6Text2new1 setLeftViewMode:UITextFieldViewModeAlways];
        [_Ans6Text2new1 setInputView:_datePicker];

    UIView *paddingView5 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
    [_ans6Text3 setClipsToBounds:NO];
    [_ans6Text3.layer setBorderWidth:1.0];
    [_ans6Text3.layer setBorderColor:[UIColor whiteColor].CGColor];
    [_ans6Text3.layer setCornerRadius:4.0];
    [_ans6Text3 setLeftView:paddingView5];
    [_ans6Text3 setLeftViewMode:UITextFieldViewModeAlways];
        UIView *paddingView5new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
        [_Ans6Text3new1 setClipsToBounds:NO];
        [_Ans6Text3new1.layer setBorderWidth:1.0];
        [_Ans6Text3new1.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_Ans6Text3new1.layer setCornerRadius:4.0];
        [_Ans6Text3new1 setLeftView:paddingView5new1];
        [_Ans6Text3new1 setLeftViewMode:UITextFieldViewModeAlways];

    UIView *paddingView6 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
    [_ans6Text4 setClipsToBounds:NO];
    [_ans6Text4.layer setBorderWidth:1.0];
    [_ans6Text4.layer setBorderColor:[UIColor whiteColor].CGColor];
    [_ans6Text4.layer setCornerRadius:4.0];
    [_ans6Text4 setLeftView:paddingView6];
    [_ans6Text4 setLeftViewMode:UITextFieldViewModeAlways];
        UIView *paddingView6new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
        [_Ans6Text4new1 setClipsToBounds:NO];
        [_Ans6Text4new1.layer setBorderWidth:1.0];
        [_Ans6Text4new1.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_Ans6Text4new1.layer setCornerRadius:4.0];
        [_Ans6Text4new1 setLeftView:paddingView6new1];
        [_Ans6Text4new1 setLeftViewMode:UITextFieldViewModeAlways];

Ans6Text1new1,Ans6Text2new1,Ans6Text3new1 and Ans6Text4new1 are the textfields which are not getting selected. Ans6Text1new1,Ans6Text2new1,Ans6Text3new1和Ans6Text4new1是未选中的文本字段。 This is code for second textfield where we get the date. 这是第二个文本字段的代码,我们在其中获取日期。

-(void)textFieldDidEndEditing:(UITextField *)textField {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *selectedDate = [formatter stringFromDate:[self.datePicker date]];
    NSString *pastDate = [formatter stringFromDate:[self.datePickerPast date]];

    if ([textField isEqual:_ans6Text2]) {
        [self.ans6Text2 setText:selectedDate];
        [self.ans6Text2 setBackgroundColor:[UIColor whiteColor]];
        [self.ans6Text2 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }
    if ([textField isEqual:_Ans6Text2new1]) {
        [self.Ans6Text2new1 setText:selectedDate];
        [self.Ans6Text2new1 setBackgroundColor:[UIColor whiteColor]];
        [self.Ans6Text2new1 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }

    else if ([textField isEqual:_ans4Text2]) {
        [self.ans4Text2 setText:pastDate];
        [self.ans4Text2 setBackgroundColor:[UIColor whiteColor]];
        [self.ans4Text2 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }
    else if ([textField isEqual:_ans7Text2]) {
        [self.ans7Text2 setText:pastDate];
        [self.ans7Text2 setBackgroundColor:[UIColor whiteColor]];
        [self.ans7Text2 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }
    else if ([textField isEqual:_ans7Text4]) {
        [self.ans7Text4 setText:pastDate];
        [self.ans7Text4 setBackgroundColor:[UIColor whiteColor]];
        [self.ans7Text4 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }
    else if ([textField isEqual:_ans7Text5]) {
        [self.ans7Text5 setText:pastDate];
        [self.ans7Text5 setBackgroundColor:[UIColor whiteColor]];
        [self.ans7Text5 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }
    else if ([textField isEqual:_ans8Text1]) {
        [self.ans8Text1 setText:pastDate];
        [self.ans8Text1 setBackgroundColor:[UIColor whiteColor]];
        [self.ans8Text1 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
    }
}

Ans6Text2new1 is the textfield which is not getting clicked. Ans6Text2new1是未单击的文本字段。 The screenshot of my Layout is below The UIView is marked in Red . 我的布局的屏幕截图在下面。UIView用红色标记 There are two UIView. 有两个UIView。 In first UIView all the TextField and button is working and in the second UIView which is inside First UIView there no Button or textfield is working. 在第一个UIView中,所有TextField和Button均正常工作,而在第一个UIView内的第二个UIView中,则没有Button或textfield工作。 This is the code for my Button click in UIView which is also not working 这是我在UIView中单击按钮的代码,这也无法正常工作

- (void)add1ChemicalTapped:(UIButton *)sender {

   NSLog(@"ADDnew tapped");
}

And in viewDidLoad 并在viewDidLoad中

- (void)viewDidLoad {
    @try{
    [super viewDidLoad];

    if(self.isReceived==true){
        [_HSSwitch setEnabled:NO];
    }


        [_Add1Chemical addTarget:self action:@selector(add1ChemicalTapped:) forControlEvents:UIControlEventTouchUpInside];
}

no need to call 无需致电

userInteractionEnabled=NO 

because if you call this for Parent UIView then it disable user interaction for child views as well. 因为如果您为Parent UIView调用它,那么也会禁用子视图的用户交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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