Cocoa: NSRectFill on button click does not work

前端 未结 3 1027
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 00:39

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:



        
3条回答
  •  天命终不由人
    2020-12-22 01:18

    well you don't have any context, the system has no idea where you want to draw. if you want to draw on a view or an image you have to use a lockFocus / unlockFocus pair.

    so if you have a view as an outlet called redView

    [redView lockFocus];
    [[NSColor redColor] set];
    NSRectFill(NSMakeRect( 50,50,10,10));
    [redView unlockFocus];
    

    but this is a really poor model, you generally want your objects to draw themselves.

    when a views drawRect: method is called you already have focus and don't need the lock unlock pair

提交回复
热议问题