Objective c drawRect未绘制NSImage
我有一个自定义视图 .h文件Objective c drawRect未绘制NSImage,objective-c,nsview,nsimage,Objective C,Nsview,Nsimage,我有一个自定义视图 .h文件 #import <Cocoa/Cocoa.h> @interface CustomView : NSView @property BOOL shallDraw; - (void) setTheShallDraw:(BOOL)draw; @end 和一个控制器类 h 现在我想从我的控制器调用NSView的SetsHallDraw,以便在rect中显示NSImage 我的问题是,尽管调用了方法setTheShallDraw,但drawRect方法并
#import <Cocoa/Cocoa.h>
@interface CustomView : NSView
@property BOOL shallDraw;
- (void) setTheShallDraw:(BOOL)draw;
@end
和一个控制器类
h
现在我想从我的控制器调用NSView的SetsHallDraw,以便在rect中显示NSImage
我的问题是,尽管调用了方法setTheShallDraw,但drawRect方法并不绘制图像。我到底错过了什么
这只是一个实验性项目,我需要这个用于一个涉及合成图像的更复杂的项目,所以在IB中使用NSImageView不会起作用。
drawRect:
在调用之前被调用[cv设置ShallDraw:YES]代码>发生
因此,在initWithFrame:
中\u应为NO代码>限制按现有方式绘制:
if (_shallDraw) {
NSLog(@"Drawing image");
在代码中,您不会将CustomView实例添加到视图层次结构中
- (IBAction)goButton:(id)sender {
CustomView *cv = [[CustomView alloc]init];
// add CustomView to view hierarchy
[theContentView addSubview:cv];
[cv setTheShallDraw:YES];
}
但是有一个[自我设置需要显示:是]
,因此应再次调用drawRect:
,使用\u shallDraw==YES
,对吗?设置shallDraw应更改为YES。SetNeedsDisplayCallDrawRect不应该再调用一次吗?哦,我错过了,请标记一个断点并检查它是否被调用两次。只需使用断点进行检查。drawRect只调用过一次。您是否收到日志消息?image
是否初始化?或者您正在向nil
发送消息?我收到了“调用方法1”的消息,但之后没有收到消息。我可能错了,因为到目前为止我只为iOS编程,但您不应该将自定义视图添加到superview吗?不确定,因为每次我从内部drawRect调用SetSharelDraw,一切正常。我应该如何声明ContentView?cv的父视图是窗口?如果使用窗口的contentView作为父视图,请使用[[window contentView]addSubview:cv]代码>
#import "Controller.h"
@implementation Controller
- (IBAction)goButton:(id)sender {
CustomView *cv = [[CustomView alloc]init];
[cv setTheShallDraw:YES];
}
@end
if (_shallDraw) {
NSLog(@"Drawing image");
- (IBAction)goButton:(id)sender {
CustomView *cv = [[CustomView alloc]init];
// add CustomView to view hierarchy
[theContentView addSubview:cv];
[cv setTheShallDraw:YES];
}