我按照一些示例使NSButton
子类工作为NSColorWell
(因为我们的NSButton
子类已经为我们提供了我们需要的外观行为),但是我注意到之后使用按钮调用面板并更改颜色,它还会更改文档中所选文本的颜色。如果我用我们的外观自定义来代替NSColorWell
,它会不会有这个问题?
然而,我仍然希望通过一种可以避免这种情况的解决方案。仍然让我们使用我们的按钮子类。我已经看到讨论线程建议让按钮本身成为第一个响应者,但是当按钮位于单独的调色板中时,我无法使其工作。此外,我宁愿不改变响应者链或让调色板成为关键窗口。 NSColorPanel
上的某个类别如何覆盖setColor:,使其发布预期通知但不触及第一个响应者?
(注意,不是简单地打开颜色面板,我现在正在使用DrummerB的BFColorPickerPopover
https://github.com/DrummerB/BFColorPickerPopover。但我不认为那是一个复杂的问题。我有同样的在整合之前NSColorPanel
/第一响应者问题。)
被要求发布源代码,所以这里是我的NSButton子类的相关位(注意,使用上面提到的选择器popover而不是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
的.m:
@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:
,或者使NSColorWell
真正做出第一响应者或其他事情吗?
答案 0 :(得分:2)
是的,一个更好的网络搜索术语( NSColorWell“第一响应者”),我看到其他人一直在与NSColorWell
和同样的问题争吵了很长时间。许多旧的邮件列表线程已经涵盖了这个并且看到了3个解决方案:
http://www.cocoabuilder.com/archive/cocoa/82832-nscolorwell-changecolor-and-first-responder.html道格拉斯戴维森建议对潜在的第一响应者进行子类化,使他们忽略changeColor:
(可能是我要做的)
http://www.cocoabuilder.com/archive/cocoa/3263-your-nscolorwell-got-in-my-nstext.html盖伊英语建议暂时将颜色作为第一响应者(我尝试了但由于颜色很好而出现问题,我不想成为关键的面板)
http://www.cocoabuilder.com/archive/cocoa/180323-detecting-currently-active-nscolorwell.html Martin建议首先通过伪装成changeColor:
并覆盖私有方法(最接近我想要的方式)来阻止NSColorPanel
来电,但风险更大应用程序商店拒绝比我很舒服)
更新:我尝试了#1但事实证明我无法覆盖第一个响应者(WebView
/ WebHTMLView
grrr)。与#3一起,我将以下内容放在NSColorPanel
类别中,将我的颜色按钮设置为panel.avoidsChangingFirstResponder=YES
,它似乎有效:
static char changeColorPatchAssociatedObjectKey; // address of this is used as a unique runtime value
- (BOOL)avoidsChangingFirstResponder
{
NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject(self, &changeColorPatchAssociatedObjectKey);
return changeColorPatchFlag && changeColorPatchFlag.boolValue;
}
- (void)setAvoidsChangingFirstResponder:(BOOL)enablePatch
{
NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject(self, &changeColorPatchAssociatedObjectKey);
if ((!changeColorPatchFlag && enablePatch) || (changeColorPatchFlag && changeColorPatchFlag.boolValue != enablePatch))
objc_setAssociatedObject(self, &changeColorPatchAssociatedObjectKey, @( enablePatch ), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (void)load
{
if (self == [NSColorPanel class])
{
// patch implementation of _forceSendAction:notification:firstResponder: (use swizzle technique from MAKVONotificationCenter.m)
// for one that calls original but with the last BOOL parameter conditionally changed to NO
SEL methodSel = NSSelectorFromString(@"_forceSendAction:notification:firstResponder:");
Method method = class_getInstanceMethod(self, methodSel);
IMP origImpl = method_getImplementation(method);
IMP newImpl = imp_implementationWithBlock(^(void *obj, SEL s, BOOL isAct, BOOL isNotif, BOOL isFirstResp) {
NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject((__bridge id)(obj), &changeColorPatchAssociatedObjectKey);
if (changeColorPatchFlag && changeColorPatchFlag.boolValue)
isFirstResp = NO;
((void (*)(void *, SEL, BOOL, BOOL, BOOL))origImpl)(obj, s, isAct, isNotif, isFirstResp);
});
class_replaceMethod(self, methodSel, newImpl, method_getTypeEncoding(method));
}
}
我希望其他人觉得这很有用。