我是NSTabView的子类,以自定义外观。我想使用NSBatonCells的NSMatrix来选择选项卡。我设法在我的NSTabView子类的initWithFrame:方法中添加带有按钮的NSMatrix。我无法工作的是以编程方式设置目标和操作。这是我试过的:
- (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");
}
视图是根据需要绘制的,但单击按钮不会调用目标方法。
我尝试以编程方式将按钮添加到标准IB视图中,如Programatically create and position an NSButton in an OS X app?
所述这很有效,但在我的自定义视图中,事情就崩溃了。任何人都能给我一个暗示我缺少的东西吗?
马丁
答案 0 :(得分:2)
尝试更改
` [_tabSelector setAction:@selector(selectedTab)];`
要
` [_tabSelector setAction:@selector(selectedTab:)];`