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 8065921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:49:07+00:00 2026-06-05T11:49:07+00:00

I have been seeing some strange behavior when I try to access a class

  • 0

I have been seeing some strange behavior when I try to access a class variable or a property in my drawRect method..

In my .h file I have the following

@interface DartBoard : UIView
{
    Board * board;
    int index;
}
@property (readwrite, assign, nonatomic) NSNumber * selectedIndex;
@end

In my .m file I have the following

@implementation DartBoard

@synthesize selectedIndex;

-(id)init
{
    self.selectedIndex = [NSNumber numberWithInt:5];
    index = 123;
    return self;
}

- (void)drawRect:(CGRect)rect {
    NSLog(@"selectedIndex: %d",[self.selectedIndex intValue]);
    NSLog(@"index: %d",index);
}

@end

the output is

2012-06-12 19:48:42.579 App [3690:707] selectedIndex: 0
2012-06-12 19:48:42.580 App [3690:707] index: 0

I have been trying to find a solution but have had no luck..

I found a similar question but there was no real answer to the issue

See: UIView drawRect; class variables out of scope

I have a feeling drawRect is different that normal methods and is not getting the scope of the class correctly but how do I fix it?

Cheers
Damien

  • 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-06-05T11:49:09+00:00Added an answer on June 5, 2026 at 11:49 am

    I have a feeling drawRect is different that normal methods and is not getting the scope of the class correctly

    No, there is nothing special about -drawRect:.

    There are two possibilities:

    1. Your -init method is not being called.

    You didn’t say how this view gets created — if you are manually calling [[DartBoard alloc] init], or if it is getting unarchived from a nib file.

    If it’s coming from a nib, UIView‘s unarchiving doesn’t know that your init method should be called. It will call the designated initializer instead, which is -initWithFrame:.

    So, you should implement that method instead, and make sure to call super!

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            self.selectedIndex = [NSNumber numberWithInt:5];
            index = 123;
        }
        return self;
    }
    

    2. There might be two instances of your view: one that you are manually initing, and another one that comes from somewhere else, probably a nib. The second instance is the one that is being drawn. Since its variables and properties are never set, they show up as zero (the default value).

    You could add this line to both your -init and -drawRect: methods, to see what the value of self is. (Or, check it using the debugger.)

    NSLog(@"self is %p", self);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been seeing some code around that resembles the following: @protocol MyProtocol <NSObject>
I'm seeing some strange behavior when I run Rails server with rails s -e
I have a system where I am seeing strange behavior with the serial ports
So I am seeing some very strange functionality in my app. I have a
I have been seeing this in some PHP script: [a-zA-Z0-9_]++ What does the double
I have been seeing some Flex Frameworks, but would like to ask to the
I have been seeing in the literature for some of the newer CPU's such
I am seeing some strange behavior with the ItemGroup tag. My application depends on
I'm writing an Android application and I've been seeing some odd behavior from Calendar
I have been seeing this a lot lately and I am unable to find

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.