iOS 之点击按钮改变状态的图片

时间:2024-05-21 16:04:44

.h  文件中

@property (strong, nonatomic) IBOutletUIButton *publishBtton;

@property (strong, nonatomic) IBOutletUIButton *timeLineButton;

@property (strong, nonatomic) IBOutletUIButton *chatButton;

@property (strong, nonatomic) IBOutletUIButton *topicButton;

.m 文件中

- (void)viewDidLoad

{

[superviewDidLoad];

_buttonArray=@[_publishBtton,_timeLineButton,_chatButton,_topicButton];

[_publishBttonsetImage:[UIImageimageNamed:@"publish"] forState:UIControlStateNormal];

[_timeLineButtonsetImage:[UIImageimageNamed:@"timeLineAfter"] forState:UIControlStateNormal];

[_chatButtonsetImage:[UIImageimageNamed:@"chatBefore"] forState:UIControlStateNormal];

[_chatButtonsetImage:[UIImageimageNamed:@"chatAfter"] forState:UIControlStateSelected];

[_topicButtonsetImage:[UIImageimageNamed:@"topicBefore"] forState:UIControlStateNormal];

[_topicButtonsetImage:[UIImageimageNamed:@"topicAfter"] forState:UIControlStateSelected];

}

-(void)changeButtonState:(UIButton *)button buttons:(NSArray *)buttonArray

{

for (UIButton* b in buttonArray)

{

b.selected=NO;

}

button.selected=YES;

}

//点击按钮事件

- (IBAction)changeView:(UIButton *)sender

{

[selfchangeButtonState:sender buttons:_buttonArray];

switch (sender.tag) {

case 400:

{

[_timeLineButtonsetImage:[UIImageimageNamed:@"timeLineBefore"] forState:UIControlStateNormal];

break;

}

case401:

{

[_timeLineButtonsetImage:[UIImageimageNamed:@"timeLineAfter"] forState:UIControlStateSelected];

break;

}

case402:

{

[_timeLineButtonsetImage:[UIImageimageNamed:@"timeLineBefore"] forState:UIControlStateNormal];

break;

}

case 403:

{

[_timeLineButtonsetImage:[UIImageimageNamed:@"timeLineBefore"] forState:UIControlStateNormal];

break;

}

default:

break;

}

}