所以我試圖做一個應用程序有一個按鈕(不一定是一個按鈕),當你將鼠標懸停在它上面彈出窗口出現。當我將鼠標懸停在按鈕上時,我已經能夠向日志打印消息,但我無法弄清楚如何將圖像的隱藏屬性設置爲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
希望這是不夠清楚
「不給對象一個委託。」此對象沒有委託,因此您要添加一個以啓用通信。你做了一個類擴展來添加委託屬性嗎?也許發佈你的-init代碼和-awakeFromNib。另一種可能性是使用NSPopover。 – 2013-02-28 23:42:11
感謝您的回覆。當我到達我的電腦時,我會發布一些代碼,但是能否快速解釋nspopover? – Ridago 2013-03-01 22:29:38
這只是一個浮動窗口,可以由用戶點擊某個東西來觸發。可能不是你想要的。檢出Apple Docs獲取更多信息,或者這裏是博客文章,http://neovibrant.com/2011/10/24/using-the-new-nspopover-since-mac-os-10-7/ – 2013-03-02 00:41:35