开发者

Reference CustomView from my Controller

This is driving me crazy. Very grateful if someone could help me out!

Problem: I have subclassed NSView (and implemented initWithRect: and drawRect:) and connected it to a customView in IB. Then in my Controller.h I am trying to create a reference to this instance by using Viewer *view; (Viewer is my subclass of NSView). However, when I try to reach a dummy function that only performs printf("something") nothing happens. Since I haven't allocated any memory for this instance [view retainCount] gives 0. My understanding was that IB would instantiate this class for me. The reason that I want to be able to reference the instance is so that I can call [view setNeedsDisplay: YES] so that the view will be redrawn. I have connected my CustomView with the view outlet in IB and saved.

#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


The steps you describe aren't entirely clear, but here are several things that stand out:

1 - It's not your place to ask an object for its -retainCount to determine whether it's being used or not. You have no way of knowing (nor are you supposed to know or depend upon knowing) what else might have an interest in this object.

2 - You check for a valid object by seeing if the object pointer ("view" in your case) is valid (points to an object) or is nil.

3 - When creating a custom NSView subclass and instantiating a copy within your nib/xib, you need to drag an NSView instance out from the library, then set its class name to that of your subclass, otherwise Interface Builder is just creating an instance of NSView. I don't think this is your problem (see #4) but you didn't say this so it's another thing to check.

4 - When you send a message to nil, nothing is exactly what is supposed to happen, so it's likely your "view" pointer/outlet is nil.

5 - It's easy to confuse "an instance I created and referenced in a nib/xib" with "an instance I created at runtime". This happens frequently with those new to Cocoa. Are you absolutely positive that the instance of the object that holds the connection (named "view") is the same as the instance you're examining at runtime? For example, you create a controller class named MyController, instantiated it in your nib/xib (as a blue cube), wired it up, etc. Then at runtime, you instantiate a new MyController ([[MyController alloc] init]...) and tried to access its (nil) "view" outlet, which points to nothing because it's not the same instance as that in your nib/xib.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜