目录
初步写按钮,发现点击状态不生效
在.h里面注册UIButton后,在viewdidLoad方法里面写如下使之生效:
//增加Cell里一个按钮
[self.contentView addSubview:({
self.deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 200, 30)];
self.deleteButton.backgroundColor = UIColor.grayColor;
self.deleteButton.selected = NO;
[self.deleteButton setTitle:@"我还没被点击" forState:UIControlStateNormal];
[self.deleteButton setTitle:@"我被摸到了(不按我就消失)" forState:UIControlStateHighlighted]; //按了不放或者按住划走都生效,点按不生效
// [self.deleteButton setTitle:@"我被点击过了" forState:UIControlStateSelected]; //这行注意,按钮的流程是先点击触发点击事件,然后在点击事件里面写这行的
self.deleteButton.font = [UIFont systemFontOfSize:12];
[self.deleteButton addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside]; //此行使得点击行为与事件关联起来
self.deleteButton;
})];
UIControlStateNormal可以理解为"UIControlStateNotSelected",未点击状态;
UIControlStateHighlighted是按了不放或者按住后再划走都生效、触发事件,点按不生效;
UIControlStateSelected点击状态。
查到监听事件,实现了按钮的点击-触发监听事件行为
监听按钮事件需要用到addtarget
和UIControlEventTouchUpInside
,在.h中注册- (IBAction)send:(id)sender;
然后在按钮代码里写如下代码关联事件:
[self.deleteButton addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside]; //此行使得点击行为与send事件关联起来
最后实现send方法,即可实现点击按钮触发send行为。
想起监听事件能做一些状态标记的改变
需要注意的是:forState:UIControlStateSelected
并非直接写在按钮视图上,而是先点击按钮触发点击事件,然后在点击事件里面更改按钮选中状态,再设置被选中状态下按钮属性(比如更改按钮标题、图片、文本颜色)。——也就是说按钮的状态完全由你决定和设置,按钮只会触发点击事件,不会自动是否选中状态。
其中上面的send方法就如下:
//按钮点击事件
- (IBAction)send:(id)sender{
NSLog(@"点击了");
[self.deleteButton setSelected:YES]; //这步几乎是必要的,点击事件模块代码
[self.deleteButton setTitle:@"我被点击过了" forState:UIControlStateSelected];
}
最终修改代码,实现按钮来回点击触发按钮文字变化
对以上代码稍作修改,可以实现按钮点击来回变动按钮文字的功能:
点击事件代码:
bool seleted = NO;
//按钮点击事件
- (IBAction)send:(id)sender{
NSLog(@"点击了");
if(seleted == YES){
[self.deleteButton setSelected:YES]; //此行非常重要
}
else{
[self.deleteButton setSelected:NO]; //此行非常重要
}
seleted = !seleted;
}
按钮视图代码:
//增加Cell里一个按钮
[self.contentView addSubview:({
self.deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 200, 30)];
self.deleteButton.backgroundColor = UIColor.grayColor;
self.deleteButton.selected = NO;
[self.deleteButton setTitle:@"我还没被点击" forState:UIControlStateNormal];
[self.deleteButton setTitle:@"我被摸到了(不按我就消失)" forState:UIControlStateHighlighted]; //按了不放或者按住划走都生效,点按不生效
[self.deleteButton setTitle:@"我被点击过了" forState:UIControlStateSelected]; //这行注意,按钮的流程是先点击触发点击事件,然后在点击事件里面写这行的
self.deleteButton.font = [UIFont systemFontOfSize:12];
[self.deleteButton addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside]; //此行使得点击行为与事件关联起来
self.deleteButton;
})];
小结
由以上可见,在视图里面所做的代码,是在视图生命周期内持续生效的,也即Target-Action模式,只要被监视的变量改变了就触发原先设定的事件。
完整代码
.m:
#import "NormalTableViewCell.h"
@interface NormalTableViewCell()
//下面开始增加label
@property(nonatomic,strong,readwrite) UILabel * titleLabel;
@property(nonatomic,strong,readwrite) UILabel * sourceLabel;
@property(nonatomic,strong,readwrite) UILabel * commentLabel;
@property(nonatomic,strong,readwrite) UILabel * timeLabel;
@property(nonatomic,strong,readwrite) UIButton * deleteButton;
@end
//本类用于被ViewController引用,将每个cell样式布置为今日头条首页cell的布局(ViewController原本cell样式是UITableView默认图片+标题+副标题样式)
//注意本类接纳了UITableViewCell协议,否则无法完美融入UITableView的Cell中
@implementation NormalTableViewCell
//重写设置每个cell样式的方法,覆盖默认每个cell布局,由于继承了UITableViewCell,所以不重写就是默认cell布局。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
self = [super initWithStyle:(UITableViewStyle)style reuseIdentifier:reuseIdentifier];
if(self){
[self.contentView addSubview:({
//gcc语法扩展,初始化方法调用、return type;
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 50, 30)]; //右、下、宽、高
// self.titleLabel.backgroundColor = UIColor.redColor;
self.titleLabel.font = [UIFont systemFontOfSize: 12];
self.titleLabel.textColor = UIColor.blackColor;
self.titleLabel;
})];
[self.contentView addSubview:({
//gcc语法扩展,初始化方法调用、return type;
self.sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 15, 50, 30)];
// self.sourceLabel.backgroundColor = UIColor.blueColor;
self.sourceLabel.font = [UIFont systemFontOfSize: 12];
self.sourceLabel.textColor = UIColor.blackColor;
self.sourceLabel;
})];
[self.contentView addSubview:({
//gcc语法扩展,初始化方法调用、return type;
self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 50, 30)];
// self.commentLabel.backgroundColor = UIColor.grayColor;
self.commentLabel.font = [UIFont systemFontOfSize: 12];
self.commentLabel.textColor = UIColor.blackColor;
self.commentLabel;
})];
[self.contentView addSubview:({
//gcc语法扩展,初始化方法调用、return type;
self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 50, 30)];
// self.timeLabel.backgroundColor = UIColor.greenColor;
self.timeLabel.font = [UIFont systemFontOfSize: 12];
self.timeLabel.textColor = UIColor.blackColor;
self.timeLabel;
})];
//增加Cell里一个按钮
[self.contentView addSubview:({
self.deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 200, 30)];
self.deleteButton.backgroundColor = UIColor.grayColor;
self.deleteButton.selected = NO;
[self.deleteButton setTitle:@"我还没被点击" forState:UIControlStateNormal];
[self.deleteButton setTitle:@"我被摸到了(不按我就消失)" forState:UIControlStateHighlighted]; //按了不放或者按住划走都生效,点按不生效
[self.deleteButton setTitle:@"我被点击过了" forState:UIControlStateSelected]; //这行注意,按钮的流程是先点击触发点击事件,然后在点击事件里面写这行的
self.deleteButton.font = [UIFont systemFontOfSize:12];
[self.deleteButton addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside]; //此行使得点击行为与事件关联起来
self.deleteButton;
})];
}
return self;
}
bool seleted = NO;
//按钮点击事件
- (IBAction)send:(id)sender{
NSLog(@"点击了");
if(seleted == YES){
[self.deleteButton setSelected:YES]; //此行非常重要
}
else{
[self.deleteButton setSelected:NO]; //此行非常重要
}
seleted = !seleted;
}
//自定义方法,写完还需要暴露此方法到接口处
- (void) showTextLabel { // 参数是:(int)a
self.titleLabel.text = @"标题";
self.commentLabel.text = @"评论数";
// self.commentLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
self.sourceLabel.text = @"资源";
self.timeLabel.text = @"时间";
}
//- (void)awakeFromNib {
// [super awakeFromNib];
// // Initialization code
//}
//
//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
// [super setSelected:selected animated:animated];
//
// // Configure the view for the selected state
//}
@end
.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface NormalTableViewCell : UITableViewCell
- (void) showTextLabel;
- (IBAction)send:(id)sender;
@end
NS_ASSUME_NONNULL_END