2013-02-28 77 views
1

所以我試圖做一個應用程序有一個按鈕(不一定是一個按鈕),當你將鼠標懸停在它上面彈出窗口出現。當我將鼠標懸停在按鈕上時,我已經能夠向日志打印消息,但我無法弄清楚如何將圖像的隱藏屬性設置爲NO。我試着給NSButtonCell委託(接收懸停事件的類),但調用讓NSButton上的懸停事件顯示圖像

[myButtonCell setDelegate:delegateObject] 

不給對象的委託。如果我能找到一個方法讓buttonCell和image進行通信(它們都在同一個xib中),或者讓buttonCell在其中一個類中調用一個函數,將它作爲一個實例,這將是一件容易的事情其餘的部分。我有一個窗口對象,有一個視圖對象,它有一個NSButtonCell對象(IBOutlet)的子類。我有一個窗口對象,它有一個NSButtonCell對象的子類。在NSButtonCell的子類中(讓我們稱之爲MyButtonCell),我有一個方法,當調用需要通知視圖或窗口時,該方法已被調用。

我覺得我到處都找,但找不到解決方案。我以爲我會委託製作,但我不能設置爲一個扣式委託所以我堅持......

編輯:

下面是NSButtonCell和委託代碼: 委託:

@interface MyView : NSView <MyButtonCellDelegate> 
    { 

    } 
@property (assign) IBOutlet MyButtonCell *buttonCell1; 
    - (void)toggleField:(int)fieldID; 
@end 

@implementation MyView 

- (void)toggleField:(int)fieldID 
{ 
    if (fieldID == 1) { 
     [self.field1 setHidden:!buttonCell1.active]; 
    } 
    NSLog(@"toggling"); 
} 
@end 

MyButtonCell:

@protocol MyButtonCellDelegate 

- (void)toggleField:(int)fieldID; 

@end 

@interface MyButtonCell : NSButtonCell 
{ 
    id <MyButtonCellDelegate> delegate; 
} 

@property BOOL active; //Used to lett the view know wether the mouse hovers over it 
@property (nonatomic, assign) id <DuErButtonCellDelegate> delegate; 

-(void)_updateMouseTracking; //mouse tracking function, if you know a better way to do it that would be lovely 

@end 

@implementation MyButtonCell 

@synthesize delegate; 
@synthesize active; 

- (void)mouseEntered:(NSEvent *)event 
{ 
    active = YES; 
    [[self delegate] toggleField:1]; 
    NSLog(@"entered"); 
} 

- (void)mouseExited:(NSEvent *)event 
{ 
    active = NO; 
    [[self delegate] toggleField:1]; 
} 

- (void)_updateMouseTracking { 
    [super _updateMouseTracking]; 
    if ([self controlView] != nil && [[self controlView] respondsToSelector:@selector(_setMouseTrackingForCell:)]) { 
     [[self controlView] performSelector:@selector(_setMouseTrackingForCell:) withObject:self]; 
    } 


} 

@end 

希望這是不夠清楚

+0

「不給對象一個委託。」此對象沒有委託,因此您要添加一個以啓用通信。你做了一個類擴展來添加委託屬性嗎?也許發佈你的-init代碼和-awakeFromNib。另一種可能性是使用NSPopover。 – 2013-02-28 23:42:11

+0

感謝您的回覆。當我到達我的電腦時,我會發布一些代碼,但是能否快速解釋nspopover? – Ridago 2013-03-01 22:29:38

+0

這只是一個浮動窗口,可以由用戶點擊某個東西來觸發。可能不是你想要的。檢出Apple Docs獲取更多信息,或者這裏是博客文章,http://neovibrant.com/2011/10/24/using-the-new-nspopover-since-mac-os-10-7/ – 2013-03-02 00:41:35

回答

1

我不知道你真正需要的,但如果我明白你問什麼在這裏:

我有一個窗口對象,以查看對象,其中有一個 NSButtonCell的子類對象(IBOutlet)。在NSButtonCell的子類 (讓我們稱之爲MyButtonCell)我有一個方法,當呼叫需要 通知視圖,或窗口,該方法已被調用。

正確,一種可能性是您的NSButtonCell將NSNotification發佈到默認通知中心,並讓您的視圖或窗口或需要知道的人成爲該通知的觀察者。您可以自由定義自己的自定義通知。

另一種可能性將是您的NSButtonCell的子類,使用方法:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 

,並從您的NSCell方法被調用時,需要通知它的視圖或窗口,可以這樣做:

[[self controlView] performSelectorOnMainThread:@selector(viewMethodToInvoke:) withObject:anObject waitUntilDone:YES] 

[[[self controlView] window] performSelectorOnMainThread:@selector(windowMethodToInvoke:) withObject:anObject waitUntilDone:YES] 

第三種可能性是按照你的建議去做,並提供你的NSButton Cell可以直接發送消息給對象,但這與在controlView或controlView的窗口上使用performSelectorOnMainThread只是一樣的事情,但更多的工作。

至於你的鼠標追蹤碼,我假設你正在使用NSTrackingArea。你可以在這裏找到它們的文檔:Using Tracking-Area Objects

+0

謝謝尋求幫助!這應該解決我的問題。 – Ridago 2013-03-02 16:57:19