Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3393554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:03:51+00:00 2026-05-18T04:03:51+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:03:52+00:00Added an answer on May 18, 2026 at 4:03 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me crazy. I have a very simple user control: public int?
This is driving me crazy. I have this one php file on a test
This is driving me crazy. I'm trying to figure out how to upload a
Okay, this is driving me crazy right now. I want to have a border
Ok, this is an embarassingly simple-looking problem, but is driving me crazy. I'm learning
This is driving me crazy. I'm not going to take it anymore. I'm going
I'm using .Net 2.0 and this is driving me crazy but there's probably some
This has been driving me crazy for a few days. Why doesn't the following
This issue is driving me mad. I have several tables defined, and CRUD stored
NHibernate is driving me 'Nuts'!!! I have the following mapping. Which returns this error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.