Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Hello,

I am new to iPhone programming, but I have done Mac programming since around 2009. I created a new "Single View" project in Xcode for the iPhone and I am attempting (without success) to add a subview. I created a subclass of UIView and called it "DrawingView". This is my DrawingView.m code:

Code:
#import "DrawingView.h"

@implementation DrawingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self setOpaque:YES];
        
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextFillRect(context, self.frame);
};


@end

The goal was to have this view be red. In the standard view controller (the Xcode generated one) I add a field:

Code:
DrawingView *drawingView;

In the viewDidLoad: of the View controller I added this code (after [super viewDidLoad]):

Code:
   CGRect screenBounds;
    screenBounds = [[UIScreen mainScreen] bounds];
    [drawingView initWithFrame:screenBounds];
    [self.view addSubview:drawingView];

When I run my app I get the standard gray screen :mad: ! Why does this code not yield a red screen?:confused:

Thanks!
 
Last edited:
You never allocated memory for your DrawingView.

Somewhere in your code you should have something like:

Code:
drawingView = [[DrawingView  alloc] initWithFrame:screenBounds];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.