2011-09-13 14 views
1

問題は、UpdateRectメソッドがdrawRectメソッドを呼び出すと、rectが高さを更新しないということです。問題NSRectを描画する

ボタンをクリックすると、矩形の高さが20になることが予想されますが、それは10のままです。なぜですか?

@implementation Graphic_view 

int height = 10; //The height of my rect. 

-(IBAction)updateRect:(id)sender { 
    height += 10; 
    //Calling the drawrect method 
    [self performSelector:@selector(drawRect:)]; 
} 

-(void)drawRect:(NSRect)dirtyRect { 
    NSLog(@"DrawRect has been called !"); 
    // Drawing code here. 
    NSRect viewBounds = [self bounds]; 
    NSColor *color = [NSColor orangeColor]; 
    [colore set]; 
    NSRectFill(viewBounds); 
    NSRect myRect; 
    myRect.origin.x = 20; 
    myRect.origin.y = 20; 
    myRect.size.height = height; 
    myRect.size.width = 100; 
    NSColor *whiteColor = [NSColor whiteColor]; 
    [whiteColor set]; 
    NSRectFill(myRect); 
} 

@end 

答えて

6

あなたは決してdrawRect:に電話をかけてはいけません。代わりにsetNeedsDisplay:

-(IBAction)updateRect:(id)sender { 
    height += 10; 
    // Schedule the drawrect method 
    [self setNeedsDisplay:YES]; 
} 

注意を呼び出す:iOSの同等物は、引数なしでsetNeedsDisplayです。

+0

ああ!ありがとうございました !しかし、私のコードを取って "[self setNeedsDisplay:YES];を挿入してください。 ?私は間違った場所に置くことを心配している;) – Alberto

+4

さあ、今。怠け者ではない、アルベルト。あなたが欠けている根本的なココアの描画コンセプトがあることを知っているので、関連ドキュメントを読むのが良いでしょう。あなたが**本当に読む必要のあるガイド**:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html –

+0

解決済み!ありがとうございました ! – Alberto

関連する問題