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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:01:44+00:00 2026-05-24T00:01:44+00:00

I have created a UIView subclass in order to implement a custom drawRect method.

  • 0

I have created a UIView subclass in order to implement a custom drawRect method. By putting some logs in the code I found that drawRect is actually getting called twice as the view first gets set up by its view controller. Why is this?

The other issue is that my UIView subclass has an ivar named needsToDrawTools. My designated initializer for this subclass sets the value of needsToDrawTools to YES. The drawRect method then checks this value. If YES, it draws the tools and then sets the value of needsToDrawTools to NO so that it never re-draws the tools.

BUT, somehow the value of needsToDrawTools is NO by the time drawRect is called. Nowhere in my code am I setting it to NO, other than from within the if(needsToDrawTools) statement inside drawRect. But since needsToDrawTools is already NO by the time it reaches that if statement, the code inside the statement never even runs. If I remove that IF statement altogether, then it does run of course and I see what I expect in the view. But I don’t want to remove the IF statement because that will result in re-drawing of things that don’t need to be re-drawn.

Here’s my code:

- (id)initWithParentViewController:(NewPhotoEditingViewController *)vc
{
    self = [super init];
    if (self) {
        parentVC = vc;
        needsToDrawTools = YES;
        NSLog(@"needsToDrawTools: %i",needsToDrawTools); //Console result: 1
    }
    return self;
}

#pragma mark - Drawing

- (void)drawRect:(CGRect)rect
{
    NSLog(@"needsToDrawTools: %i",needsToDrawTools); //Console result: 0  !!!!!
    if (needsToDrawTools){
        NSLog(@"drawingTools"); //Never shows up in the console
        UIBezierPath *toolPointDragger = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(75, 100, 30, 30)];

        [[UIColor blackColor] setStroke];
        toolPointDragger.lineWidth = 6;
        [toolPointDragger stroke];

        [[UIColor blueColor] setFill];
        [toolPointDragger fill];
        needsToDrawTools = NO;
    }
}

So again, my two questions are:

  1. Why is drawRect being called twice? I assume it gets called the first time automatically as part of the view loading process, but I don’t know why it then gets called again.

  2. How does needsToDrawTools end up with a value of NO?

  • 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-24T00:01:45+00:00Added an answer on May 24, 2026 at 12:01 am

    It sounds like you’ve got more than one instance of this view. Perhaps you’re creating one programmatically and loading one from a nib? Objective-C will set all ivars to zero (or nil, or NO) when an object is created, and if you’re loading an instance of your view from a nib, it won’t be initialized with your -(id)initWithParentViewController: and needsToDrawTools should be NO for that view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom subclass of UIView for an interface element that I
I currently have a simple iPhone app that loads a custom subclass of UIView.
I have created the following subclass to do some custom drawing: // DocumentIconView.h @interface
I have created a custom UIView that I would like to use on multiple
I created a subclass of UIView that overrides drawRect: and uses AddArcToPoint() to draw
I have created a UIView as a subview and to that subview I have
I have created a UIScrollView (canvas.scrollview) and have a custom UIView (canvas) inside it.
I currently have a UIView subclass that acts as my header view for my
I have created a subclass of UIView and I would like to have it
I created a custom UIView subclass, and would prefer to not layout the UI

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.