Why isn't changeFont: being called from my NSF

2019-05-04 15:34发布

I'm creating an NSFontPanel but selecting a font doesn't call the changeFont: method.

I have these methods defined in an NSWindowController subclass:

- (IBAction)showFontPanel:(id)sender {
    [[NSFontPanel sharedFontPanel] makeKeyAndOrderFront:self];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSFont *theFont = [NSFont fontWithName:[prefs stringForKey:iepFontName] size:[prefs floatForKey:iepFontSize]];
    [[NSFontPanel sharedFontPanel] setPanelFont:theFont isMultiple:NO];

    [[NSFontManager sharedFontManager] setDelegate:self];
}

- (void)changeFont:(id)sender {
    NSLog(@"changeFont");
}

- (NSUInteger)validModesForFontPanel:(NSFontPanel *)fontPanel {
    return NSFontPanelFaceModeMask |  NSFontPanelSizeModeMask | NSFontPanelCollectionModeMask;
}

The font panel appears with the correct font and size selected and only the modes enabled in validModesForFontPanel:, but when I select a different font, the changeFont: method doesn't get called. My understanding is that the changeFont: action message gets sent up the responder chain. As a test, I put an identical changeFont: method in my application delegate (which is supposed to be in the responder chain) but it isn't getting called either. Am I missing a step somewhere?

1条回答
我命由我不由天
2楼-- · 2019-05-04 16:11

I found an answer (http://www.cocoabuilder.com/archive/cocoa/108016-nsfontpanel-act-on-it-own-accessory-view.html#108136). I added this line:

[[NSFontManager sharedFontManager] setAction:@selector(changeDefaultFont:)];

and made the corresponding change to the method name in my NSWindowController subclass. Now when I select a different font in the font panel, changeDefaultFont: gets called.

查看更多
登录 后发表回答