0

みんな!誰かが私を助けることができれば、私はとてもうれしいです^^

問題は: クラスに IBAction メソッド (チェックボックスのクリック) があり、チェックボックスをクリックして (太字の状態に) フォント属性を変更したい。IBAction メソッドでローカル クラス変数を変更して、drawRect:メソッドでチェックしようとしましたが、機能していないため、変数は変更されません。IBAction メソッドでローカル変数を変更する方法、または別の方法がありますか? ありがとう。

#import <Foundation/Foundation.h>
@interface BigLetterView : NSView {
NSColor *bgColor;
NSString *string;
NSString *testString;
BOOL isHighlighted;
BOOL isBold;
BOOL isItalic;
IBOutlet NSButton *boldButton;
NSMutableDictionary *attributes;

}
@property(retain, readwrite) NSColor *bgColor;
@property(copy, readwrite) NSString *string;
@property(readwrite) BOOL isBold;

- (void)drawStringCenteredIn:(NSRect)r;
- (void)prepareAttributes;
- (void)changeIsBold;
- (IBAction)savePDF:(id)sender;
- (IBAction)makeBold:(id)sender; //it's that method
- (IBAction)setItalic:(id)sender;

以下の変数「isBold」が「YES」に変わります。(メソッドが呼び出されます - デバッガーでテストします)。

- (IBAction)makeBold:(id)sender
{
if ([boldButton state] == NSOnState) {
isBold = YES;
NSLog(@"Action bold=%d", isBold);
}
else {
isBold = NO;
NSLog(@"Action bold=%d", isBold);
}

}

しかし、ここで isBold はまだ「NO」です。

- (void)drawRect:(NSRect)rect {
NSRect bounds = [self bounds];
[bgColor set];
[NSBezierPath fillRect:bounds];
[self drawStringCenteredIn:bounds];
NSLog(@"isBold: %d",isBold); //HERE prints 'isBold:0'
if (isBold == YES) { 
NSFont *aFont = [attributes objectForKey:NSFontAttributeName];
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[attributes setObject:[fontManager convertFont:aFont toHaveTrait:NSBoldFontMask] forKey:NSFontAttributeName];
}

//whether this view is firstResponder
if ([[self window] firstResponder] == self && 
[NSGraphicsContext currentContextDrawingToScreen]) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[NSBezierPath fillRect:bounds];
[NSGraphicsContext restoreGraphicsState];

}
} // drawRect

アーロン・ヒレガスの本の第20章をやっているPSI。

4

1 に答える 1

0

あなたがこれまで示してきたことは良さそうに見えますが、明らかにうまくいっていないか、あなたがここにいないでしょう。-makeBold:メソッドの最後に次の行を追加してみてください。

[self setNeedsDisplay];

これにより、ビューが強制的に再描画されます。ビューが再描画されていないことが問題である可能性があります。

于 2010-12-14T18:59:03.993 に答える