UITextfield和UIButton没有在UIView中点击



我在UIView中有一些文本字段和按钮,但没有调用单击事件我已经检查了UIView的高度,它比我的按钮和文本视图更多。正如你在UIView的屏幕截图中看到的: UIView 的框架 这些是文本字段和按钮的屏幕截图 这是第一个标签

这是最后一个按钮

在两者之间有很多标签和文本字段。如此图所示 这是我的观点的图像

我也设置了

userInteractionEnabled=NO 

对于 UIView 和

userInteractionEnabled=YES;

对于文本字段和按钮,但仍然没有运气。我不知道下一步该怎么做。这是我的代码

- (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];
}

单击事件没有问题,因为在不同的UIView中调用了相同文本字段和按钮的id。所以我的 UIView 代码有些问题。这是我的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和Ans6Text4new1是未被选中的文本字段。 这是我们获取日期的第二个文本字段的代码。

-(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 是未被单击的文本字段。我的布局的屏幕截图如下 UIView 标记为红色。有两个UIView。在第一个UIView中,所有文本字段和按钮都在工作,而在第一个UIView内部的第二个UIView中,没有按钮或文本字段在工作。这是我在UIView中单击按钮的代码,它也不起作用

- (void)add1ChemicalTapped:(UIButton *)sender {
NSLog(@"ADDnew tapped");
}

在视野中DidLoad

- (void)viewDidLoad {
@try{
[super viewDidLoad];
if(self.isReceived==true){
[_HSSwitch setEnabled:NO];
}

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

无需调用

userInteractionEnabled=NO 

因为如果您为父 UIView 调用此函数,那么它也会禁用子视图的用户交互。

最新更新