2016-07-20 5 views
1

私のUI構造はNSWindow - > NSViewController - > NSView(親) - > NSView(ターゲットビュー)のようなものです。 ターゲットビューがaddSubviewではなくXIBファイルからドラッグされます。 drawRect関数は、NSView(親)が表示されているときに呼び出されますが、その後、setNeedsDisplayを使用してプログラムでdrawRectを再度呼び出すと機能しません。 私の方法には何が問題なのですか?drawRectはsetNeedsDisplayで呼び出されません

とにかく、これを実装するのにdrawRect(view.frame)を使うことができますが、これは良い考えではないと思います。

コード:

MyView.m 

- (void)drawRect:(NSRect)dirtyRect { 
    [super drawRect:dirtyRect]; 

    NSLog(@"asasa"); 
} 

... 
@property (weak) IBOutlet MyView *titleBarView; 
... 

-(IBAction)test:(id)sender 
{ 
    NSLog(@"%@",self.view.subviews); // contains the titleBarView 
    NSLog(@"%@",self.titleBarView); // not nil 
    [self.titleBarView setNeedsDisplay:YES]; // not call drawRect 
} 

titleBarViewがすでにself.viewself.titleBarViewdrawRect:をトリガしませんが、今やっていること

+0

あなたはターゲットビューの 'setNeedsDisplay'を呼び出しますか? – bluedome

+0

@bluedome、はい、私はsetNeedsDisplayを呼び出しましたが、動作しません – jimwan

+0

'setNeedsDisplay:'を呼び出すコードを表示します。また、あなたがあなたがいると思うビューでそれを呼んでいると確信していますか?そのビュー、親またはターゲットはどのビューですか?何か(コンセントなど)を正しく接続していないため、そのビューへの参照が「なし」になることはありますか? 'drawRect'を呼び出さないでください。それは正しく動作しません。 –

答えて

1

に接続されています。 drawRect:self.viewNSLogステートメントの文)にするには、でsetNeedsDisplayと呼び出します。

- (void)drawRect:(NSRect)dirtyRect { 
[super drawRect:dirtyRect]; 

NSLog(@"asasa"); 
} 

... 
@property (weak) IBOutlet MyView *titleBarView; 
... 

-(IBAction)test:(id)sender 
{ 
    NSLog(@"%@",self.view.subviews); // contains the titleBarView 
    NSLog(@"%@",self.titleBarView); // not nil 
    [self.titleBarView setNeedsDisplay:YES]; // this triggers the drawRect: method of self.titleBarView 
    [self.view setNeedsDisplay:YES]; // this triggers your drawRect: method with he NSLog statement 
} 
+0

問題は '[self.titleBarView setNeedsDisplay:YES]'はself.titleBarViewのdrawRect:メソッドを呼び出さない – jimwan

+0

ああ。 Hm。 self.titleBarViewがビューの階層に追加されていますか? –

+0

drawRect関数で 'CAGradientLayer * gradientLayer = [CAGradientLayer layer];' 'self.layer = gradientLayer; 'を使用した根本的な原因が見つかりましたが、なぜこのコードがこの問題を引き起こすのかわかりません – jimwan

関連する問題