有没有办法让 NSNumberFormatter(或可能是任何其他 NSFormatter)在 NSPopover 中工作?
popover 中 NSTextField 的值绑定到 NSViewController 的代表对象。当在字段中输入无效数字(例如,“asdf”)时,会在包含显示弹出窗口的 NSView 的 NSWindow 中显示一个说明该值无效的工作表。
单击“确定”后,您将获得以下回溯:
* thread #1: tid = 0x4e666a, 0x00007fff931f9097 libobjc.A.dylib`objc_msgSend + 23, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x00007fff931f9097 libobjc.A.dylib`objc_msgSend + 23
frame #1: 0x00007fff8a1fa6c8 AppKit`-[NSTextView(NSSharing) becomeKeyWindow] + 106
frame #2: 0x00007fff8a080941 AppKit`-[NSWindow(NSWindow_Theme) acquireKeyAppearance] + 207
frame #3: 0x00007fff8a0800df AppKit`-[NSWindow becomeKeyWindow] + 1420
frame #4: 0x00007fff8a07f5c6 AppKit`-[NSWindow _changeKeyAndMainLimitedOK:] + 803
frame #5: 0x00007fff8a1a205d AppKit`-[NSWindow _orderOutAndCalcKeyWithCounter:stillVisible:docWindow:] + 1156
frame #6: 0x00007fff8a0876c5 AppKit`-[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 3123
frame #7: 0x00007fff8a0867f0 AppKit`-[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 786
frame #8: 0x00007fff8a086470 AppKit`-[NSWindow orderWindow:relativeTo:] + 162
frame #9: 0x00007fff8a1a1425 AppKit`__18-[NSWindow _close]_block_invoke + 443
frame #10: 0x00007fff8a1a1230 AppKit`-[NSWindow _close] + 370
frame #11: 0x00007fff8a2d0565 AppKit`__106-[NSApplication(NSErrorPresentation) presentError:modalForWindow:delegate:didPresentSelector:contextInfo:]_block_invoke3221 + 50
frame #12: 0x00007fff8a2d02f7 AppKit`-[NSApplication(NSErrorPresentation) _something:wasPresentedWithResult:soContinue:] + 18
frame #13: 0x00007fff8a28fe9d AppKit`-[NSAlert didEndAlert:returnCode:contextInfo:] + 90
frame #14: 0x00007fff8a28f8c2 AppKit`-[NSWindow endSheet:returnCode:] + 368
frame #15: 0x00007fff8a28f49d AppKit`-[NSAlert buttonPressed:] + 107
frame #16: 0x00007fff8a1543d0 AppKit`-[NSApplication sendAction:to:from:] + 327
frame #17: 0x00007fff8a15424e AppKit`-[NSControl sendAction:to:] + 86
frame #18: 0x00007fff8a1a0d7d AppKit`-[NSCell _sendActionFrom:] + 128
frame #19: 0x00007fff8a1ba715 AppKit`-[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2316
frame #20: 0x00007fff8a1b9ae7 AppKit`-[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 487
frame #21: 0x00007fff8a1b91fd AppKit`-[NSControl mouseDown:] + 706
frame #22: 0x00007fff8a13ad08 AppKit`-[NSWindow sendEvent:] + 11296
frame #23: 0x00007fff8a0d9744 AppKit`-[NSApplication sendEvent:] + 2021
frame #24: 0x00007fff89f29a29 AppKit`-[NSApplication run] + 646
frame #25: 0x00007fff89f14803 AppKit`NSApplicationMain + 940
objc_msgSend 中崩溃时的寄存器是:
(lldb) reg read
General Purpose Registers:
rax = 0x0000610000190740
rbx = 0x0000610000190740
rcx = 0x0000000000000080
rdx = 0x00007fff8a97fd93 "currentEditor"
rdi = 0x0000610000190740
rsi = 0x00007fff8a9612bf "respondsToSelector:"
rbp = 0x00007fff5fbfeae0
rsp = 0x00007fff5fbfeab8
r8 = 0x000000000000002e
r9 = 0xffff9fffffeb1bbf
r10 = 0x00007fff8a9612bf "respondsToSelector:"
r11 = 0xbaddbe5c3e96bead
r12 = 0x0000610000053830
r13 = 0x00007fff931f9080 libobjc.A.dylib`objc_msgSend
r14 = 0x000060000012a500
r15 = 0x00007fff931f9080 libobjc.A.dylib`objc_msgSend
rip = 0x00007fff931f9097 libobjc.A.dylib`objc_msgSend + 23
rflags = 0x0000000000010246
cs = 0x000000000000002b
fs = 0x0000000000000000
gs = 0x00000000c0100000
我猜这是因为显示工作表后瞬态弹出窗口的窗口消失了,当前编辑器和任何可以响应选择器的对象也是如此。
将弹出框行为设置为 NSPopoverBehaviorSemitransient 会有所帮助,但如果弹出框因文本字段中的无效值而被解除,则仍会引发异常。
在这一点上,为了避免这个问题,我所能想到的就是手动验证数值。呸。
更新 1
正如 Brian Webster 在下面发现的,这是 AppKit 的一个基本问题。
由于我的验证需求非常简单(只是正整数),因此解决方法是在 KVC 对象中进行手动验证,该对象用作 NSPopover 显示的 NSViewController 中的代表对象。由于 NSTextField确实想使用字符串值,因此使用 -valueForKey: 和 -setValue:forKey: 来转换标量值。当您为文本字段中的绑定值打开“立即验证”时,只要文本字段发生更改,就会调用验证方法。
(在你问之前,一个 NSValueTransformer 不能做这个工作,因为它不参与验证过程。它只会在填充字段或保存更改时被调用。一旦用户输入了一些无效的内容,我就想要反馈数据——就像 NSFormatter 一样。)
这是我所做的要点:
- (id)valueForKey:(NSString *)key
{
if ([key isEqualToString:@"property1"]) {
return [NSString stringWithFormat:@"%zd", _property1];
}
else if ([key isEqualToString:@"property2"]) {
return [NSString stringWithFormat:@"%zd", _property2];
}
else {
return [super valueForKey:key];
}
}
- (BOOL)validateValue:(inout id *)ioValue forKey:(NSString *)inKey error:(out NSError **)outError
{
if (! *ioValue) {
*ioValue = @"0";
}
else if ([*ioValue isKindOfClass:[NSString class]]) {
NSString *inputString = [[(NSString *)*ioValue copy] autorelease];
inputString = [inputString stringByReplacingOccurrencesOfString:@"," withString:@""];
NSInteger integerValue = [inputString integerValue];
if (integerValue < 0) {
integerValue = -integerValue;
}
*ioValue = [NSString stringWithFormat:@"%zd", integerValue];
}
return YES;
}
- (void)setValue:(id)value forKey:(NSString *)key
{
if ([value isKindOfClass:[NSString class]]) {
if ([key isEqualToString:@"property1"]) {
_property1 = [value integerValue];
}
else if ([key isEqualToString:@"property2"]) {
_property2 = [value integerValue];
}
else {
[super setValue:value forKey:key];
}
}
else {
[super setValue:value forKey:key];
}
}
现在我需要洗个澡。
更新 2
感谢@PixelCutCompany 关于他们如何在 PaintCode 应用程序中做事的一些有用提示:
https://twitter.com/PixelCutCompany/status/441695942774104064 https://twitter.com/PixelCutCompany/status/441696198140125184
我想出了这个:
@interface PopupNumberFormatter : NSNumberFormatter
@end
@implementation PopupNumberFormatter
- (BOOL)getObjectValue:(out id *)anObject forString:(NSString *)aString range:(inout NSRange *)rangep error:(out NSError **)error
{
NSNumber *minimum = [self minimum];
NSNumber *maximum = [self maximum];
if (aString == nil || [aString length] == 0) {
if (minimum) {
*anObject = minimum;
}
else if (maximum) {
*anObject = maximum;
}
else {
*anObject = [NSNumber numberWithInteger:0];
}
}
else {
if (! [super getObjectValue:anObject forString:aString range:rangep error:nil]) {
// if the superclass can't parse the string, assign a reasonable default
if (minimum) {
*anObject = minimum;
}
else if (maximum) {
*anObject = maximum;
}
else {
*anObject = [NSNumber numberWithInteger:0];
}
}
else {
// clamp the parsed value to a minimum and maximum (if set)
if (minimum && [*anObject compare:minimum] == NSOrderedAscending) {
*anObject = minimum;
}
else if (maximum && [*anObject compare:maximum] == NSOrderedDescending) {
*anObject = maximum;
}
}
}
return YES;
}
@end
基本上,您可以通过始终提供有效值来避免工作表或对话框出现问题。上面的代码在分配默认值时会考虑最小值和最大值。子类还考虑了 nil 或空字符串以及钳位值。
这让我感觉不那么脏。