【问题标题】:UITextfield and UIButton not clicking inside UIViewUITextfield 和 UIButton 没有在 UIView 内点击
【发布时间】:2019-02-28 23:54:48
【问题描述】:

我在 UIView 中有一些文本字段和按钮,但点击事件没有被调用我检查了 UIView 的高度,它比我的按钮和文本视图更多。正如你在 UIView 的截图中看到的: Frame of UIView 这些是 TextField 和按钮的屏幕截图 This is the first Label

And this is the last button

在两者之间有很多标签和文本字段。正如你在这张图片中看到的 This is the image of my view

我也设置了

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 是没有被点击的文本字段。我的布局截图如下 The UIView is marked in Red。有两个 UIView。在第一个 UIView 中,所有 TextField 和按钮都在工作,而在 First 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 将使其子项的输入事件被禁用。所以不要那样做。
  • @RyanB 我已将 userInteractionEnabled 设置为 YES 仍然没有被点击。
  • 那么您的UIView 容器的其他代码是什么?只是按钮操作的一部分代码就很难知道发生了什么。
  • 我已添加代码
  • 还是和你的其他问题一样。你的_chemicalUIview 是什么以及如何被初始化。您说_Ans6Text1new1 是动态添加的,但是您是如何以及在哪里执行此操作的?

标签: ios objective-c uiview uibutton


【解决方案1】:

不用打电话

userInteractionEnabled=NO 

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多