【问题标题】:drawRect not called with setNeedsDisplay未使用 setNeedsDisplay 调用 drawRect
【发布时间】:2016-07-20 14:59:58
【问题描述】:

我的 ui 结构就像 NSWindow -> NSViewController -> NSView(parent) -> NSView(target view)。 目标视图是从 XIB 文件中拖动的,而不是 addSubviewdrawRect 函数将在 NSView(parent) 显示时被调用,但之后我想用setNeedsDisplay 再次以编程方式调用 drawRect,它不起作用。 我的方法有什么问题?

无论如何,我可以使用 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 已经连接

【问题讨论】:

  • 你是否调用目标视图的setNeedsDisplay
  • @bluedome,是的,我调用了 setNeedsDisplay 但它不起作用
  • 显示调用setNeedsDisplay:的代码。另外,你确定你是按照你认为的观点来称呼它的吗?那是哪个视图,父视图还是目标视图?会不会是因为您没有正确连接某些东西(例如插座),所以您对该视图的引用是 nil?千万不要打电话drawRect;它不会正常工作。
  • @KenThomases,请查看我的更新
  • 我可以使用[self.view setNeedsDisplay:YES]来调用NSView(Parent)的drawRect

标签: cocoa


【解决方案1】:

您现在正在执行的操作将触发drawRect:self.titleBarView,但不会触发self.view。要触发self.viewdrawRect:(带有NSLog 语句的那个​​),请在self.view 上调用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
}

【讨论】:

  • 问题是[self.titleBarView setNeedsDisplay:YES]没有调用self.titleBarView的drawRect:方法
  • 哦。嗯。您确定 self.titleBarView 已添加到视图层次结构中吗?
  • 我找到了我在drawRect函数中使用CAGradientLayer *gradientLayer = [CAGradientLayer layer];self.layer = gradientLayer;的根本原因,但我不知道为什么这段代码会导致这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多