2014-05-07 44 views
0

我正在繼承NSTabView以定製外觀。我想使用NSButtonCells的NSMatrix來選擇選項卡。我設法在我的NSTabView子類的initWithFrame:方法中添加帶有按鈕的NSMatrix。我無法工作的是以編程方式設定目標和行動。這裏是我的嘗試:在自定義視圖中添加按鈕(NSRadioModeMatrix)的NSMatrix,設置目標/動作

定義TAB_WIDTH 24.0f

定義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"); 
} 

根據需要的圖繪,但點擊按鈕不會調用對象的方法。

我已經試過這裏描述Programatically create and position an NSButton in an OS X app?

這一工程以編程按鈕添加到標準的IB視圖,但事情在我的自定義視圖土崩瓦解。任何人都可以給我一個暗示我缺少的東西嗎?

馬丁

+0

猜測:嘗試使用'id'而不是'NSMatrix *'作爲sender'的''爲在selectTab'類型 - - 我不確定這裏的「發件人」是矩陣還是嵌入式按鈕。它可能是通過找出哪個按鈕是發送者來確定被點擊的內容的範例。 – stevesliva

回答

2

嘗試改變

` [_tabSelector setAction:@selector(selectedTab)];` 

` [_tabSelector setAction:@selector(selectedTab:)];` 
+0

是的,它做到了。謝謝你的幫助! – Martin

+1

@Martin如果你還在身邊,你應該接受這是正確的答案。 –

+0

沒什麼大不了的。他受益。其他人也可能受益。對於第一次使用該成語的用戶來說,解決方案是一個錯字或容易遺漏冒號。 – uchuugaka

相關問題