【问题标题】:The touch event not working for button and textview in iOS Objective-C触摸事件不适用于 iOS Objective-C 中的按钮和文本视图
【发布时间】:2018-09-24 11:38:05
【问题描述】:

我有一个按钮和一个 Textview,我在单击添加按钮时动态添加它们。但是我添加的新文本视图和按钮无法识别我的触摸。我已经创建了事件识别,但仍然没有处理触摸事件。 The touch in the textview which is marked in red is not working

“添加和删除”按钮也不起作用。我为此创建了触摸事件,但函数本身没有被调用,这意味着它没有采用触摸手势。这是我的代码

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

    _ans6Constraint.constant = 340;
    [self reloadTable];

}



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

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

            [_addChemical addTarget:self action:@selector(addChemicalTapped:) forControlEvents:UIControlEventTouchUpInside];
             [_RemoveChemical1 addTarget:self action:@selector(RemoveChemical1Tapped:) forControlEvents:UIControlEventTouchUpInside];

         // NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:_ans6Text4 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
        UILabel *titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        [titleLbl setText:@"Questionnaire"];
        [titleLbl setTextColor:[UIColor whiteColor]];
        [titleLbl setFont:[UIFont fontWithName:@"OpenSans-Semibold" size:17.0]];
        [self.navigationItem setTitleView:titleLbl];

        [self.navigationController setNavigationBarHidden:NO];
        [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:63.0/255.0 green:29.0/255.0 blue:11.0/255.0 alpha:1.0]];
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc]initWithCustomView:backButton];
        [self.navigationItem setLeftBarButtonItem:backBarButton];

        if (!self.isLiteNVD) {
            UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
            [saveButton setImage:[UIImage imageNamed:@"PropertyFence"] forState:UIControlStateNormal];
            [saveButton addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
            [self.navigationItem setRightBarButtonItem:saveBarButton];
        }


        _datePicker = [[UIDatePicker alloc]init];
        [_datePicker setBackgroundColor:[UIColor whiteColor]];
        [_datePicker setDatePickerMode:UIDatePickerModeDate];
        [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];

        _datePickerPast = [[UIDatePicker alloc]init];
        [_datePickerPast setBackgroundColor:[UIColor whiteColor]];
        [_datePickerPast setDatePickerMode:UIDatePickerModeDate];
        [_datePickerPast setMaximumDate:[NSDate date]];
        [_datePickerPast addTarget:self action:@selector(pastDateChanged:) forControlEvents:UIControlEventValueChanged];

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

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

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

        [_ans4ImageView setClipsToBounds:YES];
        [_ans4ImageView.layer setBorderWidth:1.0];
        [_ans4ImageView.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_ans4ImageView.layer setCornerRadius:4.0];

        [_ans4Button setClipsToBounds:NO];
        [_ans4Button.layer setBorderWidth:1.0];
        [_ans4Button.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_ans4Button.layer setCornerRadius:4.0];

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

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

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

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

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

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

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

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



        [_ans9Text1.layer setBorderWidth:1.0];
        [_ans9Text1.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_ans9Text1.layer setCornerRadius:4.0];
        [_lastAnswers.layer setCornerRadius:4.0];
        [_ntButton.layer setCornerRadius:4.0];
        [_resetAll.layer setCornerRadius:4.0];
        [_msaBtn.layer setCornerRadius:4.0];
        [_gapButton.layer setCornerRadius:4.0];
        [_neButton.layer setCornerRadius:4.0];

        [_submitButton.layer setBorderWidth:1.0];
        [_submitButton.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_submitButton.layer setCornerRadius:4.0];

        [_HSBtn.layer setCornerRadius:4.0];
        [_nfasBtn.layer setCornerRadius:4.0];


          //[self.tableView beginUpdates];
           // [self.view addConstraints:@[ top]];
    //        [self.view bringSubviewToFront:_addChemical];
    //
    //        [self reloadTable];
    //        [self.tableView reloadData];
           // [self.tableView bringSubviewToFront: _addChemical];
    //        NSArray *arr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:_questionArr.count-1 inSection:0]];
    //
    //        [self.tableView insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
            //[self.tableView endUpdates];

        NSDictionary *attrDict = @{
                                   NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:13.0],
                                   NSForegroundColorAttributeName : [UIColor yellowColor]
                                   };
        NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"I agree with declaration * "];
        [string addAttributes:attrDict range:NSMakeRange(0,25)];
        [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(25,1)];
        [string addAttribute:NSExpansionAttributeName value:@-1 range:NSMakeRange(25,1)];
        [_DeclarationLbl setAttributedText:string];
        [_consDeclarationBtn.layer setCornerRadius:4.0];

        [_signatureView.layer setBorderWidth:1.0];
        [_signatureView.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_signatureView.layer setCornerRadius:4.0];

        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 100;

        NSDictionary *userDic = [[DBHelper getSharedInstance] getCurrentUser];

        if (self.operationType == OperationType_Quick){

            if ([self.answersDic objectForKey:@"ResponsibleContactSignature"]) {
                UIImage *consImage = [self.answersDic objectForKey:@"ResponsibleContactSignature"];
                self.signImg.image = consImage;
                [self.consDeclarationSwitch setOn:YES];
            }
            else if (![[userDic objectForKey:@"UserSignaturePath"] isKindOfClass:[NSNull class]]) {
                NSString *dbPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                UIImage *consImage = [UIImage imageWithContentsOfFile:[dbPath stringByAppendingPathComponent:[userDic objectForKey:@"UserSignaturePath"]]];
                self.signImg.image = consImage;
                [self.consDeclarationSwitch setOn:YES];
            }
            else{
                [self.signImg setImage:[UIImage imageNamed:@"signature"]];
            }
        }
        else if (self.operationType == OperationType_Edit){

            if (![[self.NVDDic objectForKey:@"ResponsibleContactSignature"] isKindOfClass:[NSNull class]]) {
                NSString *ENVDPath = [[DBHelper getSharedInstance] ENVDPath];
                UIImage *consImage = [UIImage imageWithContentsOfFile:[ENVDPath stringByAppendingPathComponent:[self.NVDDic objectForKey:@"ResponsibleContactSignature"]]];
                self.signImg.image = consImage;
            }
            else{
                [self.signImg setImage:[UIImage imageNamed:@"signature"]];
            }
            if (![[self.NVDDic objectForKey:@"DeclarerAcknowledgement"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"DeclarerAcknowledgement"] boolValue]) {
                [self.consDeclarationSwitch setOn:YES];
            }
        }
        else if (![[userDic objectForKey:@"UserSignaturePath"] isKindOfClass:[NSNull class]]) {

            NSString *dbPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            UIImage *consImage = [UIImage imageWithContentsOfFile:[dbPath stringByAppendingPathComponent:[userDic objectForKey:@"UserSignaturePath"]]];
            self.signImg.image = consImage;
            [self.consDeclarationSwitch setOn:YES];
        }
        else{
            [self.signImg setImage:[UIImage imageNamed:@"signature"]];
        }

        [self.nfasSwitch setEnabled:NO];
        [_nfasBtn setHidden:YES];


        if (![[self.NVDDic objectForKey:@"HasNFASQuestion"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"HasNFASQuestion"] boolValue]) {
            self.hasNFAS=TRUE;
           // [_nfasSwitch setOn:YES];
           //[_nfasBtn setHidden:NO];
            [_nfasSwitch setOn:YES];
            [_nfasBtn setHidden:YES];
        }
        else{
            [_nfasSwitch setOn:NO];
            [_nfasBtn setHidden:YES];
        }

        if (![[self.NVDDic objectForKey:@"HasHealthStatement"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"HasHealthStatement"] boolValue]) {
            if(self.isReceived==true){
                [_HSSwitch setOn:YES];
                [_HSBtn setHidden:YES];
               self.isReceivedHasHealthStatement=true;
             }else{
                 [_HSSwitch setOn:YES];
                 [_HSBtn setHidden:NO];
             }
        }
        else{
            [_HSSwitch setOn:NO];
            [_HSBtn setHidden:YES];
        }

        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"CanAccessAccreditation"]) {

            [self.ntButton setHidden:YES];

            [_msaBtn setHidden:YES];
            [_msaSwitch setOn:NO];
            [_msaSwitch setHidden:YES];
            [_msaLbl setHidden:YES];

            [_hasGAP setHidden:YES];
            [_hasGAP setOn:NO];
            [_gapButton setHidden:YES];
            [_gapLbl setHidden:YES];

            [_hasNE setHidden:YES];
            [_hasNE setOn:NO];
            [_neButton setHidden:YES];
            [_neLbl setHidden:YES];
            //[_NFASView setHidden: FALSE];

            _signatureConstraint.constant = 10;
            //_NFASHeightConstraint.constant = 0;

            [_NVDDic setObject:[NSNumber numberWithBool:NO] forKey:@"HasMSAQuestion"];
        }
        else {

            int height = 10;

            if (([self.NVDDic objectForKey:@"ConsignorState"] && ![[self.NVDDic objectForKey:@"ConsignorState"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"ConsignorState"] isEqualToString:@"NT"]) || ([self.NVDDic objectForKey:@"DestinationState"] && ![[self.NVDDic objectForKey:@"DestinationState"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"DestinationState"] isEqualToString:@"NT"]) || ([self.NVDDic objectForKey:@"HasNTWaybill"] && [[self.NVDDic objectForKey:@"HasNTWaybill"] boolValue])) {

                [self.ntButton setHidden:NO];

                height = height + 46;
            }
            else{
                [self.ntButton setHidden:YES];
            }

            if (![[self.NVDDic objectForKey:@"HasMSAQuestion"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"HasMSAQuestion"] boolValue]) {
                [_msaSwitch setOn:YES];
                [_msaBtn setHidden:NO];

                _msaConstraint.constant = height;
                height = height + 46;
            }
            else{
                [_msaSwitch setOn:NO];
                [_msaSwitch setHidden:YES];
                [_msaBtn setHidden:YES];
                [_msaLbl setHidden:YES];
            }

            if (![[self.NVDDic objectForKey:@"IsGAPAccredited"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"IsGAPAccredited"] boolValue]) {
                [self.hasGAP setOn:YES];
                [self.gapButton setHidden:NO];

                _gapConstraint.constant = height;
                height = height + 46;
            }
            else{
                [_hasGAP setOn:NO];
                [_hasGAP setHidden:YES];
                [_gapButton setHidden:YES];
                [_gapLbl setHidden:YES];
            }

            if (![[self.NVDDic objectForKey:@"IsNEAccredited"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"IsNEAccredited"] boolValue]) {
                [_hasNE setOn:YES];
                [_neButton setHidden:NO];

                _neConstraint.constant = height;
                height = height + 46;
            }
            else{
                [_hasNE setOn:NO];
                [_hasNE setHidden:YES];
                [_neButton setHidden:YES];
                [_neLbl setHidden:YES];
            }
            _signatureConstraint.constant = height;
        }

        if (![[self.NVDDic objectForKey:@"HasESC"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"HasESC"] boolValue]) {
            [_hasESC setOn:YES];
        }
        else{
            [_hasESC setOn:NO];
        }

        if (![[self.NVDDic objectForKey:@"HasTPA"] isKindOfClass:[NSNull class]] && [[self.NVDDic objectForKey:@"HasTPA"] boolValue]) {
            [_hasTPA setOn:YES];
        }
        else{
            [_hasTPA setOn:NO];
        }

        if ([self.answersDic allKeys].count > 0) {
            [self loadQuestionaires];
        }

        if (_operationType != OperationType_Edit) {

            [_hasGAP setOn:NO];
            [self showGAP:_hasGAP];

            [_hasNE setOn:NO];
            [self showNE:_hasNE];
        }

        if (self.arrNFASSummary != nil){
            for (int i = 0; i < [self.arrNFASSummary count];i++){
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[self.arrNFASSummary objectAtIndex:i]];
                if ([dict valueForKey:@"Dentition"] != nil)
                    [dict setValue:@"" forKey:@"Dentition"];
                if ([dict valueForKey:@"FeedDays"] != nil)
                    [dict setValue:[NSNumber numberWithInteger:0] forKey:@"FeedDays"];
            }
        }

        NSString *statementQuery = [NSString stringWithFormat:@"SELECT * FROM CattleHealthStatementDetail WHERE NVDId = '%@'",[self.NVDDic objectForKey:@"NVDId"]];
        NSDictionary *dictHS = [[DBHelper getSharedInstance] getDetailsBySQL:statementQuery];
        if (dictHS != nil){
            [_NVDDic setObject:dictHS forKey:@"HSAnswers"];
        }
        }
        @catch (NSException *exception) {
            NSLog(@"%@", exception.reason);
        }
        }

我认为UI代码有问题。这是点击段时调用视图的代码我认为我必须在此处进行更改

else if ([sender isEqual:_ans6Segment]) {

        if (_ans6Segment.selectedSegmentIndex == 1) {

            _ans6Text1.text = @"";
            _Ans6Text1new1.text=@"";
            _ans6Text2.text = @"";
          _Ans6Text2new1.text=@"";
            _ans6Text3.text = @"";
            _Ans6Text3new1.text=@"";
            _ans6Text4.text = @"";
            _Ans6Text4new1.text=@"";
            _ans6Constraint.constant = 5;
        }
        else {

            _ans6Constraint.constant = 340;

            [self.tableView bringSubviewToFront: _addChemical];
            NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:_addChemical attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
            [self.view addConstraints:@[ top]];
        }
    }

还有一个很大的空白区域出现在视图中我不知道它来自哪里是否与这个问题有关我已经尝试减小 Y 轴和宽度以移除该空白区域但没有任何效果你可以看到在 This image 我正在发布我的布局的屏幕截图。

This is the Screenshot

我知道我的 UIView 类的布局有问题。因为文本字段的 id 可以在不同 Layout 的其他文本字段中访问。

【问题讨论】:

    标签: ios objective-c uibutton uitextview


    【解决方案1】:

    在许多情况下,任何视图都在文本字段和按钮上...这就是为什么您看起来像单击按钮但实际上您单击的是前视图(不幸的是在前面添加了)。

    请用以下代码确认您的触摸事件代码

    [AddButton addTarget:self action:@selector(RemoveChemical1Tapped:) forControlEvents:UIControlEventTouchUpInside];
    

    也检查此代码

    [storeCatalog addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:storeCatalog action:@selector(onTouch:)]];
    

    我希望这对你有用...

    【讨论】:

      【解决方案2】:

      默认情况下,当您触摸UITextView 时,iOS 会触发UITextViewTextDidBeginEditingNotification 通知。通过为该通知设置观察者,您将能够处理您的UITextView 上的触摸。

      注册UITextViewTextDidBeginEditingNotification

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:textView];
      

      还有你的处理程序:

      -(void)handleTextBeginEditing:(UITextView *)sender {
          if ([sender isEqual:textView]) {
              NSLog(@"TextView tapped");
          }
      }
      

      对于按钮,我认为使用addTarget 函数捕获触摸事件没有问题。尝试在您调用addTarget 时检查_RemoveChemical1

      【讨论】:

      • UI 滚动视图不允许我处理任何触摸事件,这段代码对我有用吗?
      • - (void)addChemicalTapped:(UIButton *)sender { _ans6Constraint.constant = 750; // [self.addChemical addTarget:self action:@selector(fixDelaysContentTouches:) forControlEvents:UIControlEventTouchUpInside]; [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(handleTextBeginEditing:) name:UITextViewTextDidBeginEditingNotification 对象:_Ans6Text1new1]; [自重载表]; }
      • -(void)handleTextBeginEditing:(UITextView *)sender { if ([sender isEqual:_Ans6Text1new1]) { NSLog(@"TextView tapped"); } }
      • handleTextBeginEditing 在调试器中也没有受到影响
      • addChemicalTapped: 是否被调试器命中?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 2013-01-02
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多