cocoa:设置图片的多个按钮,Tab效果

时间:2012-05-03 12:47:55

标签: objective-c macos cocoa

有几个按钮,单击其中一个,更改此按钮的图片,其他按钮设置另一个图片。以下代码哪里有错误?

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);

enter image description here

1 个答案:

答案 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"]];}
  }