Cocoa NSView赢得';t通过bezierpath绘制

Cocoa NSView赢得';t通过bezierpath绘制,cocoa,nsview,nsbezierpath,Cocoa,Nsview,Nsbezierpath,亲爱的可可程序员们 我想完成的事情: - (IBAction)isChecked:(id)sender { //if myChekcbox is checked, show the pop up button if ([sender state]==NSOnState) { NSLog(@"Checked"); [myPopUp setHidden:NO]; } else { //if the checkbox is unchecked, hide t

亲爱的可可程序员们

我想完成的事情:

- (IBAction)isChecked:(id)sender {
  //if myChekcbox is checked, show the pop up button
  if ([sender state]==NSOnState) {
    NSLog(@"Checked");
    [myPopUp setHidden:NO];
  }
  else
  {
    //if the checkbox is unchecked, hide the popupbutton
    [myPopUp setHidden:YES];
    NSLog(@"Unchecked");

  }
  //reload my drawrect method (reload the view)
  [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
  //if the checkedbutton is checked, draw the line
  if ([myCheckbox state]==NSOnState)
  {
    NSBezierPath *myPath = [NSBezierPath bezierPath];
    [myPath moveToPoint:NSMakePoint(10, 20)];
    [myPath lineToPoint:NSMakePoint(50, 20)];
    [myPath setLineWidth:2];
    [myPath stroke];
  }

}
我的画布上有一个复选框、一个弹出按钮(隐藏)和一个NSView。 如果选中myCheckbox->显示弹出按钮,并在NSView上通过bezierPath绘制一条线。 如果未选中myCheckbox->再次隐藏弹出按钮并“取消绘制”路径

代码:

- (IBAction)isChecked:(id)sender {
  //if myChekcbox is checked, show the pop up button
  if ([sender state]==NSOnState) {
    NSLog(@"Checked");
    [myPopUp setHidden:NO];
  }
  else
  {
    //if the checkbox is unchecked, hide the popupbutton
    [myPopUp setHidden:YES];
    NSLog(@"Unchecked");

  }
  //reload my drawrect method (reload the view)
  [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
  //if the checkedbutton is checked, draw the line
  if ([myCheckbox state]==NSOnState)
  {
    NSBezierPath *myPath = [NSBezierPath bezierPath];
    [myPath moveToPoint:NSMakePoint(10, 20)];
    [myPath lineToPoint:NSMakePoint(50, 20)];
    [myPath setLineWidth:2];
    [myPath stroke];
  }

}
问题:

- (IBAction)isChecked:(id)sender {
  //if myChekcbox is checked, show the pop up button
  if ([sender state]==NSOnState) {
    NSLog(@"Checked");
    [myPopUp setHidden:NO];
  }
  else
  {
    //if the checkbox is unchecked, hide the popupbutton
    [myPopUp setHidden:YES];
    NSLog(@"Unchecked");

  }
  //reload my drawrect method (reload the view)
  [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
  //if the checkedbutton is checked, draw the line
  if ([myCheckbox state]==NSOnState)
  {
    NSBezierPath *myPath = [NSBezierPath bezierPath];
    [myPath moveToPoint:NSMakePoint(10, 20)];
    [myPath lineToPoint:NSMakePoint(50, 20)];
    [myPath setLineWidth:2];
    [myPath stroke];
  }

}
如果选中state=nson,则弹出按钮可见,但线条无法绘制,我想知道为什么。。。我个人认为这是一个连接问题

我在这里上传了项目文件(相当小,35kb):

全球: 我读过NSView文档,它说只有一种方法可以绘制视图,那就是通过drawRect方法。这是真的吗?这也是绘制视图的下降方式吗?(如果视图中的函数和设置需要显示:方法中的是)

提前感谢,,
Ben

您需要获得一个
NSColor
实例,然后调用它来设置当前笔划颜色。它不知道在
drawRect:
的开头使用哪种颜色来绘制路径,因此您必须告诉它。

Jesper,代码运行了,但它不会执行“技巧”héhé。我刚刚将Drawing.zip更改为正确的链接,请原谅。这很奇怪,因为线条绘制时没有IF-statement。发现问题:这是一个连接问题,自我设置需要显示:是的应该是:myView设置需要显示:是的,非常愚蠢!谢谢你的帮助