Cocoa / Objective-C:单击按钮时绘制矩形

发布于 2024-08-18 10:14:40 字数 445 浏览 0 评论 0原文

大家好,stackoverflow 的人们, 我对可可还很陌生。我有 XCode 3.1

情况: 我有一个 NSObject 子类:(AppController),带有一个链接到按钮的操作。 比我有一个自定义视图,连接到我的 NSView 子类(AppView),在drawRect命令中我绘制一个矩形(所有这些东西都有效), 我在 AppView.ma 函数中 - (void) drawIt { .. } 绘制矩形。 现在我在 - (void) drawRect ... 中用 [self drawIt] 调用它。这也有效。

我现在想做的是在单击按钮时调用drawIt。 (在 AppController.m 中,当 Action -(IBAction) ... 由于按钮单击而被调用时)

我希望你能帮助我, 我是 stackoverflow 的新手,所以我不知道我是否应该在这里传递所有代码,我可以,但也许这样更容易阅读

Hellow stackoverflow people,
I am pretty new to Cocoa. I have XCode 3.1

Situation:
I have a NSObject subclass: (AppController) with an action, linked to a button.
Than i have a custom View, connected to my NSView subclass (AppView), in the drawRect command i draw a rectangle (all that stuff works),
i have in the AppView.m a function - (void) drawIt { .. } which draws the rectangle.
For now i called it in the - (void) drawRect ... with [self drawIt]. That works too.

What i want to do now is to call drawIt when the button is clicked. (in the AppController.m when the Action -(IBAction) ... is called due to a button Click)

I hope u can help me,
I am new to stackoverflow so i dont know wether i should past all the code here, i can but maybe its easier to read like this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-08-25 10:14:40

您应该阅读 Cocoa 绘图指南 概念材料。当系统认为有必要时,系统会要求您的视图-drawRect:。在这方面,您可以随时要求自己绘制视图。因此,你必须从“绘制当前状态”的角度来思考这一点。

您可能应该做的(在这种基本情况下)可能是为您的自定义视图提供一个布尔属性“drawIt”,并让您的按钮操作在视图实例上切换此属性。这样if (self.drawIt == YES),您就可以调用矩形绘制代码。

当调用 -drawRect: 时,您应该始终执行一些操作来“清除”视图(例如用白色填充整个边界),然后仅在满足条件时才绘制有条件的内容。

示例:

- (void)drawRect:(NSRect)aRect
{
  // Clean up background (we ignore "aRect", drawing entire view at once)
  [[NSColor whiteColor] set];
  NSRectFill([self bounds]);

  // Do we want to draw our magic rect?
  if ([self drawMagicRect])
  {
    [[NSColor redColor] set];
    NSRectFill([self magicRect]);
  }
}

You should read the Cocoa Drawing Guide conceptual material. Your view is asked to -drawRect: by the system when the system feels it's necessary. In that regard, your view can be asked to draw itself at any time. Therefore, you have to think of this in terms of "drawing the current state".

What you should probably do (in this basic situation) is perhaps give your custom view a boolean property "drawIt" and have your button action toggle this on the view instance. This way if (self.drawIt == YES), you can call your rectangle-drawing code.

You should always do something to "clear" the view when -drawRect: is called (like fill the whole bounds with white), then only draw the conditional stuff if the condition is met.

Example:

- (void)drawRect:(NSRect)aRect
{
  // Clean up background (we ignore "aRect", drawing entire view at once)
  [[NSColor whiteColor] set];
  NSRectFill([self bounds]);

  // Do we want to draw our magic rect?
  if ([self drawMagicRect])
  {
    [[NSColor redColor] set];
    NSRectFill([self magicRect]);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文