ios - UICollectionReusableView 中的 UIBUtton 操作
问题描述
我在 UICollectionReusableView 中添加 UIButton。我可以添加按钮及其操作,但按钮操作不起作用。我尝试过添加 TapGesture 并启用 UICollectionReusableView 和按钮的用户交互,但没有效果。下面是我的代码
- (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;
}
下面是点击手势
//MARK:- Link button Action
-(void)linkButtonPressed
{
OrganizationVC *vc = [[OrganizationVC alloc] init];
vc.signupFlow = YES;
[_parent.navigationController pushViewController:vc animated:YES];
}
我也通过了下面的链接
请帮助我我做错了什么。
解决方案
在 UIButtons 上使用内部修饰而不是手势识别器
[linkAccountButton addTarget:self action:@selector(linkButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
然后动作应该是:
-(void) linkButtonPressed:(UIButton*)sender
推荐阅读
- ruby - 制作机架 CLI
- haskell - haskell 中的“case maybeVal of”的多个“Just x”
- gtmetrix - 请求之间的 Gt Metrix 时间
- java - 是java中new关键字后面的类的构造函数吗?
- boolean - Grafana / influxDB:在选定期间,指标值 = 1 有多少分钟?
- javascript - Express js 与 mysql 仅更新传递的字段
- docker - 在 docker 容器启动中运行多个命令
- c# - 如何在 c# linq 中拆分字符串?
- c# - Asp.net MVC5 身份
- python - 打印张量的所有内容