Gagan_iOS
Gagan_iOS

Reputation: 4070

UIBUtton Action in UICollectionReusableView

I am adding UIButton inside UICollectionReusableView. I can add button & it's action but button action is not working. I have tried by adding TapGesture & enable user interaction of UICollectionReusableView & button as well but no effect. Below is my code

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor menigaLightBackgroundColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
        imageView.userInteractionEnabled = YES;
        imageView.image = [UIImage imageNamed:@"Login_115"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.tag = 10;
        [self addSubview:imageView];

        UIImage *titleImage  = [UIImage imageNamed:@"titleLogo"];
        UIImageView *imageViewTitle = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleImage.size.width, titleImage.size.height)];
        imageViewTitle.userInteractionEnabled = YES;
        imageViewTitle.center = self.center;
        imageViewTitle.contentMode = UIViewContentModeScaleAspectFit;
        imageViewTitle.image = titleImage;
        [self addSubview:imageViewTitle];

        float margin = 15.0;
        //Title Label  //10 is the tag for Arch image view
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(margin, CGRectGetMaxY([self viewWithTag:10].frame)+ 10 , CGRectGetWidth(self.frame)-margin*2, 60.0)];
        titleLabel.text = @"Your wallets have been linked successfully. Link more accounts now to fully understand your spending.";
        titleLabel.numberOfLines = 4;
        titleLabel.userInteractionEnabled = YES;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont getDINRegularFontOfSize:16];
        titleLabel.textColor = [UIColor menigaDarkTextColor];
        [self addSubview:titleLabel];

        //Buttons
        linkAccountButton = [[CustomButtonClass alloc] initWithFrame:CGRectMake(margin,  CGRectGetMaxY(titleLabel.frame)+ 10, CGRectGetWidth(self.frame)-margin*2, 44.0)];
        linkAccountButton.userInteractionEnabled = YES;
        linkAccountButton.layer.cornerRadius = 22.0;
        [linkAccountButton setTitle:@"Link An Account" forState:UIControlStateNormal];
        [linkAccountButton.titleLabel setFont:[UIFont getRobotFontOfSize:14]];
        [linkAccountButton setBackgroundColor:[UIColor menigaBrandColorRed]];
        [self addSubview:linkAccountButton];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(linkButtonPressed)];
        tapGesture.numberOfTapsRequired = 1;
        [linkAccountButton addGestureRecognizer:tapGesture];

        [linkAccountButton buttonTouchUpInsideWithCompletion:^{
            MENIGAChooseOrganizationVC *vc = [[MENIGAChooseOrganizationVC alloc] init];
            vc.signupFlow = YES;
            [_parent.navigationController pushViewController:vc animated:YES];
        }];

        //set button hidden
        titleLabel.hidden = YES;
        linkAccountButton.hidden = YES;

    }
    return self;
}

and below is Tap gesture

//MARK:- Link button Action
-(void)linkButtonPressed
{
   OrganizationVC *vc = [[OrganizationVC alloc] init];
    vc.signupFlow = YES;
    [_parent.navigationController pushViewController:vc animated:YES];
}

I have also gone through below link

Link

Please help me what am I doing wrong.

Upvotes: 1

Views: 236

Answers (1)

Benhamine
Benhamine

Reputation: 153

Use touch up inside instead of a gesture recognizer on UIButtons

[linkAccountButton addTarget:self action:@selector(linkButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

and then the action should be:

-(void) linkButtonPressed:(UIButton*)sender

Upvotes: 1

Related Questions