2010-12-14 5 views
0

みんな! 誰もが、私は^非常に喜んでいるよ私を助けることができる場合)ローカルクラス変数IBActionを変更するには

問題がある: 私は私のクラスでIBActionメソッド(チェックボックスをクリック)を持っていると私は大胆な状態に(チェックボックスをクリックして、フォント属性を変更したいです) 。 私は私の中でそれをチェックするためIBActionメソッド内のローカルクラス変数を変更しようとしている - のdrawRect:方法を、それが働いていないで - 変数は変更を行いません。 IBActionメソッドでローカル変数を変更する方法や別の方法がありますか?おかげさまで ここで 'YES' から 'isBold' 変化変数以下

#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; 

。 (メソッド呼び出し - 私はデバッガでテストします)。

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

} 

ただし、ここではisoldはまだ 'いいえ'です。

- (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章を行っています。あなたが示されてきたもの

+3

コードを表示してください。特に、あなたの行動方法が本当に呼び出されているかどうかを確認してください。 –

+0

コメントありがとう、Ole!それを行った。 – Alexander

答えて

0

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

[self setNeedsDisplay]; 

これは再描画自体にビューを強制します。あなたの問題は、ビューが再描画されていないことだけである可能性があります。

+0

答えてくれてありがとう、ジェフ! 1分前にそれを試してみました。 - 何も変わらない。問題は、私はIBActionで変数を変更しようとし、別の方法でこの変数の値をチェックしようとしています。 IBAction(私はログ印刷)の変数の変化が、別の方法で変数の古い値インチ(IBActionの変数やクラスの残りの部分は別のメモリ空間を持っているように見える。.. – Alexander

+0

がどのようにビューを作成している? –

+0

まず私は、クラスを作成し、 NSViewから継承し、IBで 'Custom View'をドラッグし、Class IdentityでデフォルトのNSViewを以前に作成したクラスに設定しました。 – Alexander

関連する問題