示例代码
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.layer.masksToBounds = YES;
button.layer.cornerRadius = 3;
[button setTitle:@"25" forState:UIControlStateNormal];
[button setTitle:@"26" forState:UIControlStateSelected];
[button addTarget:self action:@selector(touchZan:) forControlEvents:UIControlEventTouchUpInside];
- (void)touchZan:(UIButton*)button {
button.selected = !button.selected;
}
讲解
- 首先要说明的是一点,UIButton的selected属性必须要为设置才能生效,所以要添加一个点击事件,加入button.selected = !button.selected;
- 另外对于状态的设定直接放在上面就好,不要放在点击事件里,可能会导致需要点击两次才会改变的情况