为什么不从我的NSFontPanel调用changeFont

为什么不从我的NSFontPanel调用changeFont

本文介绍了为什么不从我的NSFontPanel调用changeFont :?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在创建 NSFontPanel ,但是选择字体不会调用 changeFont:方法。



我在 NSWindowController 子类中定义了以下方法:

 -(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;
}

字体面板显示正确的字体和大小,并且仅启用模式在 validModesForFontPanel:中,但是当我选择其他字体时,不会调用 changeFont:方法。我的理解是 changeFont:操作消息是在响应者链上发送的。作为测试,我在应用程序委托中放置了相同的 changeFont:方法(该方法应该在响应者链中),但没有被调用。我在某个地方错过了一步吗?

解决方案

我找到了答案()。我添加了这一行:

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

,并在我的 NSWindowController 子类。现在,当我在字体面板中选择其他字体时,会调用 changeDefaultFont:


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.

这篇关于为什么不从我的NSFontPanel调用changeFont :?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:52