[英]UITextfield and UIButton not clicking inside UIView
我在UIView中有一些文本字段和按鈕,但是沒有調用click事件,我檢查了UIView的高度超過了按鈕和textview的高度。 如您在UIView的屏幕截圖中所見:UIView的框架這些是TextField和按鈕的屏幕截圖這是第一個Label
在它們之間有很多標簽和文本字段。 如您在此圖像中看到的, 這是我視圖的圖像
我也已經設定
userInteractionEnabled=NO
對於UIView和
userInteractionEnabled=YES;
用於TextField和Button,但仍然沒有運氣。 我不知道下一步該怎么辦,這是我的代碼
- (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];
}
單擊事件沒有問題,因為同一文本字段和按鈕的ID在不同的UIView中被調用。 所以我的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中,所有TextField和Button均正常工作,而在第一個UIView內的第二個UIView中,則沒有Button或textfield工作。 這是我在UIView中單擊按鈕的代碼,這也無法正常工作
- (void)add1ChemicalTapped:(UIButton *)sender {
NSLog(@"ADDnew tapped");
}
並在viewDidLoad中
- (void)viewDidLoad {
@try{
[super viewDidLoad];
if(self.isReceived==true){
[_HSSwitch setEnabled:NO];
}
[_Add1Chemical addTarget:self action:@selector(add1ChemicalTapped:) forControlEvents:UIControlEventTouchUpInside];
}
無需致電
userInteractionEnabled=NO
因為如果您為Parent UIView調用它,那么也會禁用子視圖的用戶交互。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.