我遵循一些例子,使NSButton
子類作爲NSColorWell
(因爲我們的NSButton
子類已經給我們我們需要的外觀行爲),但我注意到後使用按鈕調用面板並更改顏色,它也會更改文檔中選定文本的顏色。如果我將NSColorWell
與我們的外觀定製區分開來,它會不會有這個問題?NSButton子類作爲colorwell&防止NSColorPanel觸摸第一響應者
但是,我仍然希望能夠避免&仍然讓我們使用我們的按鈕子類。我已經看到討論線程建議讓按鈕本身成爲第一響應者,但是當按鈕處於單獨的調色板中時,我無法使其工作。此外,我不想改變響應者鏈或調色板成爲關鍵窗口。 NSColorPanel
上的一個類別如何超過setColor:,使其發佈預期通知但不觸及第一響應者?
(注意,而不是簡單地打開彩色面板,我目前通過DrummerB https://github.com/DrummerB/BFColorPickerPopover利用的BFColorPickerPopover
,但我不認爲這是多大的併發症。我之前整合有同樣的NSColorPanel
/急救員問題它)。
被要求發佈源代碼,所以這裏從我NSButton子類的相關位(注意,用途,而不是NSColorPanel
直接上述選擇器酥料餅):
.H:
@interface ...
@property (nonatomic, strong) NSColor *color;
@property (nonatomic, assign) BOOL active;
@property (nonatomic, strong) NSColor *buttonColor;
@property (nonatomic, weak) BFColorPickerPopover *popover;
- (void)activate:(BOOL)exclusive; // param ignored, always exclusive
- (void)activate;
- (void)deactivate;
- (void)takeColorFrom:(id)sender;
@end
。米:
@implementation ...
@dynamic color;
- (NSColor *)color
{
return self.buttonColor;
}
- (void)setColor:(NSColor *)newColor
{
self.buttonColor = newColor;
[self generateSwatch];
self.needsDisplay = YES;
self.popover.color = newColor;
}
- (void)activate:(BOOL)exclusive
{
[self activate]; // always exclusive
}
- (void)activate
{
self.popover = [BFColorPickerPopover sharedPopover];
self.popover.color = self.buttonColor;
[self.popover showRelativeToRect:self.frame ofView:self.superview
preferredEdge:self.preferredEdgeForPopover];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(popoverDidClose:)
name:NSPopoverDidCloseNotification
object:self.popover];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(colorDidChange:)
name:NSColorPanelColorDidChangeNotification
object:self.popover.colorPanel];
activeButton = self;
self.active = YES;
}
- (void)deactivate
{
if (self.popover)
{
[self.popover close];
self.popover = nil;
}
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSPopoverDidCloseNotification object:self.popover];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSColorPanelColorDidChangeNotification
object:self.popover.colorPanel];
if (activeButton == self) activeButton = nil;
self.active = NO;
}
- (void)popoverDidClose:(NSNotification *)notification
{
self.popover = nil; // don't let deactivate ask it to close again
[self deactivate];
}
- (void)colorDidChange:(NSNotification *)notification
{
self.buttonColor = self.popover.colorPanel.color;
[self generateSwatch];
self.needsDisplay = YES;
[self sendAction:self.action to:self.target];
}
- (void)mouseDown:(NSEvent *)theEvent
{
if (self.isEnabled && !self.active)
[self activate];
else if (self.active)
[self deactivate];
}
- (void)takeColorFrom:(id)sender
{
if ([sender respondsToSelector:@selector(color)])
self.color = [sender color];
}
@end
附錄:
我嘗試使用正常的NSColorWell
來代替我的NSButton子類和相同的問題。除了調用操作方法外,面板中選擇的顏色還會調用第一響應者的changeColor:
。因此,在我的問題中忘記了關於NSButton
的一切,一般來說,如何使用NSColorWell
,其顏色不得也被推送到第一響應者身上?必須要求定製預期的第一響應者,以選擇性地忽略changeColor:
,或者使第一響應者真的是該做的事情或其他事情?
發佈您的代碼。 –