I am subclassing NSTabView to customise the appearance. I want to use an NSMatrix of NSButtonCells to select the tabs. I managed to add the NSMatrix with buttons in the initWithFrame: method of my NSTabView subclass. What I can not get to work is setting the target and action programmatically. Here is what I tried:
define TAB_WIDTH 24.0f
define TAB_HEIGHT 24.0f
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
NSInteger numberOfTabs = 5;
NSInteger tabSpacing = 8;
NSSize cellSize = NSMakeSize(TAB_WIDTH, TAB_HEIGHT);
NSSize interCellSpacing = NSMakeSize(tabSpacing, 0);
CGFloat tabSelectorWidth = TAB_WIDTH * numberOfTabs + tabSpacing * numberOfTabs - 1;
CGFloat xOrigin = (frameRect.size.width - tabSelectorWidth) / 2;
NSRect tabSelectorFrame = NSMakeRect(xOrigin, 0, tabSelectorWidth, TAB_HEIGHT);
NSButtonCell *cellPrototype = [[NSButtonCell alloc] init];
[cellPrototype setBordered:NO];
_tabSelector = [[NSMatrix alloc] initWithFrame:tabSelectorFrame
mode:NSRadioModeMatrix
prototype:cellPrototype
numberOfRows:1
numberOfColumns:5];
[_tabSelector setTarget:self];
[_tabSelector setAction:@selector(selectedTab)];
[_tabSelector setCellSize:cellSize];
[_tabSelector setIntercellSpacing:interCellSpacing];
NSArray *theCells = [_tabSelector cells];
[theCells[0] setImage:[NSImage imageNamed:@"tab1"]];
[theCells[1] setImage:[NSImage imageNamed:@"tab2"]];
[theCells[2] setImage:[NSImage imageNamed:@"tab3"]];
[theCells[3] setImage:[NSImage imageNamed:@"tab4"]];
[theCells[4] setImage:[NSImage imageNamed:@"tab5"]];
[self addSubview:_tabSelector];
[self setDrawsBackground:NO];
[self setTabViewType:NSNoTabsNoBorder];
}
return self;
}
- (void)selectTab:(NSMatrix *)sender
{
NSLog(@"selected tab");
}
The view is drawn as desired, but clicking the buttons does not call the target method.
I have tried to add buttons programmatically to a standard IB view as described here Programatically create and position an NSButton in an OS X app?
That works, but things fall apart in my custom view. Can anybody give me a hint what I am missing?
Martin