小弟调调
小弟调调

Reputation: 1333

cocoa:Multiple buttons to set the picture,Tab effect

There are several buttons,Click on one of,Change the picture of this button,Other button to set another picture。The following code where there is an error?

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

Add testbuttonnull

NSLog(@"button tag: %ld %d %@", tag,i ,button);

enter image description here

Upvotes: 0

Views: 226

Answers (1)

Ashish Chauhan
Ashish Chauhan

Reputation: 1376

use this code

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

Upvotes: 1

Related Questions