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?
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.
来源:https://stackoverflow.com/questions/6147305/why-isnt-changefont-being-called-from-my-nsfontpanel