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

  • Home
  • SEARCH
  • 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 8539729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:26:08+00:00 2026-06-11T11:26:08+00:00

I’m trying to follow a guide on how to draw some shapes on screen

  • 0

I’m trying to follow a guide on how to draw some shapes on screen but at the app launch it works fine but I can’t “re-draw” the shapes using setNeedsDisplay I’ve tried a lot of stuff floating around like executing on main thread but it doesn’t work.

My App is Made of this:

enter image description here

My UIView has it’s own class, DrawView. Here is my code:

DrawView.h

#import <UIKit/UIKit.h>

NSInteger drawType;

@interface DrawView : UIView

-(void)drawRect:(CGRect)rect;
-(void)drawNow:(NSInteger)type;

@end

DrawView.m

#import "DrawView.h"

@implementation DrawView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *color = [UIColor orangeColor];
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextSetFillColorWithColor(context, color.CGColor);

    NSLog(@"Type: %i",drawType);

    switch (drawType) {
            case 0:
                CGContextMoveToPoint(context, 10, 100);
                CGContextAddLineToPoint(context, 300, 300);
                CGContextSetLineWidth(context, 2.0);
                CGContextStrokePath(context);
                break;
            case 1:
                CGContextAddEllipseInRect(context,CGRectMake(10, 100, 300,440));
                CGContextDrawPath(context, kCGPathFillStroke);
                break;
            case 2:
                CGContextAddRect(context, CGRectMake(10, 100,300,300));
                CGContextDrawPath(context, kCGPathFillStroke);
                break;
            default:
                break;
    }

}


-(void)drawNow:(NSInteger)type {
    drawType = type;
    NSLog(@"Draw Now! %i",drawType);

    //[self setNeedsDisplay]; // Not working...
    //[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES]; // Not Working
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "DrawView.h"

DrawView *mydraw;

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *drawTypeSW;

- (IBAction)drawNow:(id)sender;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize drawTypeSW;

- (void)viewDidLoad
{
    [super viewDidLoad];
    mydraw = [[DrawView alloc] init];
}

- (void)viewDidUnload
{
    [self setDrawTypeSW:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)drawNow:(id)sender {
    [mydraw drawNow:drawTypeSW.selectedSegmentIndex];
}
@end

When I open the app a line is draw but when I try to draw something else using the button Draw Now it doesn’t work, nothing happens and - (void)drawRect:(CGRect)rect is not called. Why? What am I missing?

Thank you 😉

  • 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-11T11:26:10+00:00Added an answer on June 11, 2026 at 11:26 am

    Further Edit, final resolution: The DrawView being created in viewDidLoad wasn’t being added as a subview to the ViewController. Adding the following line should make it appear correctly (in addition to other fixes below):

    [self.view addSubview:mydraw];
    

    Edit: An extension to my answer (which still stands, but I think isn’t the core of the problem anymore). You’re declaring myDraw outside of the interface (and doing something similar in DrawView’s header file). Instead of

    DrawView *mydraw;
    
    @interface ViewController : UIViewController
    //snip
    @end
    

    try:

    @interface ViewController : UIViewController
    {
    DrawView *mydraw;
    }
    //snip
    @end
    

    Original answer:
    The variable myDraw hasn’t been properly initialised – UIViews need to be initialised using either initWithFrame (for programmatic creation) or initWithCoder (when in a nib or storyboard). Using init to create it means it has a frame location/size of 0 (and other UIView functionality may be not initalised properly), which in turn means it won’t draw properly.

    Try either a) creating a UIView in your nib/storyboard, turning it into a DrawView, and making the drawView field in your ViewController definition an outlet for that view. Or b) Remove it from your nib, create your DrawView using initWithFrame, specifying a location and size on the screen for it.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.