Why isn't changeFont: being called from my NSFontPanel?

时光总嘲笑我的痴心妄想 提交于 2019-12-22 08:58:03

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!