這是推動我瘋了。非常感謝有人能幫助我!參考CustomView從我的控制器
問題: 我已經子類的NSView(和實施initWithRect:
和drawRect:
),並將其連接到一個IB customView。然後在我的Controller.h中,我試圖通過使用Viewer *view;
(查看器是我的NSView的子類)創建對此實例的引用。但是,當我試圖達到僅執行printf("something")
的虛擬功能時,什麼都不會發生。因爲我沒有爲這個實例分配任何內存[view retainCount]給出0.我的理解是IB會爲我實例化這個類。我希望能夠引用該實例的原因是,我可以撥打[view setNeedsDisplay: YES]
以便重新繪製視圖。我已將自定義視圖連接到IB的視圖插口並保存。
#import "Viewer.h"
#import "Controller.h"
@implementation Viewer
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
return self;
}
-(void)awakeFromNib
{
printf("awake!\n"); //works!
}
- (void)drawRect:(NSRect)rect
{
CGContextRef myContext = [[NSGraphicsContext currentContext]graphicsPort];
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
printf("%f\n",gPopulation[i][j]/2);
CGContextSetRGBFillColor (myContext, gPopulation[i][j]/2, 0.3, 0.1, 1); // Set color
CGContextFillRect (myContext, CGRectMake (i*50, j*50, 50, 50));
}
}
}
**- (void) redraw { //dummy function that I can't reach from controller with [view redraw]. Gives no error, but retainCount = 0**
printf("redraw------\n");
//[self display];
}
@end
#import <Cocoa/Cocoa.h>
#import "Viewer.h"
double gPopulation[8][8];
@interface Controller : NSObject {
NSMutableArray *emptySpots;
int nEmpty, nWhite, nBlack;
NSOperationQueue *queue;
IBOutlet Viewer *view;
}
- (void) main;
- (id) initWithMain;
- (void) updatePopulation;
- (void) initPopulation;
@end
感謝您的回答, – salomons 2010-11-10 20:24:30
更新了更多的心思。 – 2010-11-10 20:26:07
1.確定 2.嘗試 如果(圖==無){ 的printf( 「isNil」); } 而事實上它是零。奇怪,因爲我已經連接了自定義視圖(我改變了不是NSView,但查看器[我的子類])與我的控制器類中的插座視圖。它應該真的是零? 3.我將CustomView的副本拖到我的窗口中。這一定是一回事,對嗎? – salomons 2010-11-10 20:29:44