有几个按钮,单击其中一个,更改此按钮的图片,其他按钮设置另一个图片。以下代码哪里有错误?
NSButton *button = (NSButton *)sender;
NSInteger tag = button.tag;
[button setImage:[NSImage imageNamed:@"menuBtnHover.png"]];
for (int i = 5; i <= 8; i++) {
if (tag != i) {
button =[(NSButton *)[self contentView] viewWithTag:i];
[button setImage:[NSImage imageNamed:@"menuBtn.png"]];
}
}
添加测试,button
= null
NSLog(@"button tag: %ld %d %@", tag,i ,button);
答案 0 :(得分:1)
使用此代码
NSButton *button = (NSButton *)sender;
NSInteger tag = button.tag;
[button setImage:[NSImage imageNamed:@"menuBtnHover.png"]];
for (int i = 5; i <= 8; i++) {
if (tag != i) { button =[(NSButton *)[self contentView] viewWithTag:i];
if(button)[button setImage:[NSImage imageNamed:@"menuBtn.png"]];}
}