Cocoa:NSRectFill的按钮点击不工作 [英] Cocoa: NSRectFill on button click does not work

查看:176
本文介绍了Cocoa:NSRectFill的按钮点击不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在窗口中有一个按钮,下面是我的AppDelegate.m文件:

  @implementation LGAppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSColor redColor] set];
NSRectFill(NSMakeRect(50,50,10,10));
}


- (IBAction)buttonPressed:(id)sender
{
[[NSColor greenColor] set];
NSRectFill(NSMakeRect(60,60,10,10));
}
@end

我期望看到的是一个矩形,应用程序启动,单击按钮后另一个矩形显示。但是,只有一个矩形显示,单击按钮后没有发生。
请帮我解决这个问题。谢谢。
Yours,
Z

解决方案

一些背景, applicationDidFinishLaunching:

但是在这一点上,可能有许多窗口和许多视图由应用程序显示。只需调用 NSRectFill()是不够的。应用程序不知道在哪里绘制矩形(是在这个窗口还是那个?这个NSView或那个)。即使只有一个窗口,它可能看起来明显...可能有多个NSViews显示,你不知道....和计算机,你必须非常明确的命令。



底线是:没有为绘图操作发生的位置建立上下文。正如 d00dle 指出,您应该阅读绘图指南



当调用NSView的 drawRect:时,已经设置/定义了一个上下文您可以直接从您的委托的 applicationDidFinishLaunching:绘制,但需要定义一个上下文。


Hello I am new to Cocoa programming and I met a problem about NSRectFill.

There is one button in the window, and the following is my AppDelegate.m file:

@implementation LGAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSColor redColor] set];
    NSRectFill(NSMakeRect( 50,50,10,10));
}


- (IBAction)buttonPressed:(id)sender
{
    [[NSColor greenColor] set];
    NSRectFill(NSMakeRect( 60,60,10,10));
}
@end

What I expected to see is a rectangle shows when the application starts, and another rectangle shows after clicking the button. However, only one rectangle shows, nothing happened after clicking the button. Please help me to solve this. Thank you. Yours, Z

解决方案

Some background, applicationDidFinishLaunching: is called at the launch of a program once the program is 'done' loading resources.

But at this point, there could be many windows and many views displayed by the application. Simply calling NSRectFill() is not enough. The application wouldn't know where to draw said rectangle (is it in this window or that one? This NSView or that one). Even if there's only one window and it might seem obvious... there might be multiple NSViews displayed that you're unaware of.... and with computers you have to be very explicit with your commands.

The bottom line is: there is no 'context' established for where the drawing actions to occur. As d00dle points out, you should read up on Drawing Guide.

When an NSView's drawRect: is called, a context (itself) has already been set/defined. You could draw directly from your delegate's applicationDidFinishLaunching: but a 'context' needs to be defined.

这篇关于Cocoa:NSRectFill的按钮点击不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆