Link to home
Start Free TrialLog in
Avatar of waffe
waffeFlag for United States of America

asked on

drawRect inside of a UIViewControll

Hi,

I have looked of the drawRect method inside of UIView and I have downloaded and executed the apple docs code. It works fine but I want to have my app draw when a user selects a button from a UIViewController. I tried for a while but I can never call the drawRect method inside of UIView. So how do I produce drawn images from UIView and put them in a UIViewController.

I have googled for days and I'm stuck - any pointers and/or tutorial links on how this is done would be great!

Thanks,
waffe
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

You need to call setNeedsDisplay and drawRect will be called automatically.
[yourView setNeedsDisplay:YES];
Avatar of waffe

ASKER

Thanks,

I am still having problems getting it to work.

I don't think I'm working with the drawRect class correctly, can you please take a look?

Here is part of my RootViewController that has the button that calls DrawRect:
.h--------------------------------------------------------------
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController{
}

@end

.m--------------------------------------------------------------
#import "RootViewController.h"
#import "DrawRect.h"

@implementation RootViewController

- (IBAction) playBtn:(id)sender;


- (IBAction) playBtn:(id)sender {
      NSLog(@"Button Pressed");
      
      DrawRect *myRectView = [DrawRect alloc];
      [myRectView setNeedsDisplay:YES];            
}

DrawRect
.h--------------------------------------------------------------
#import <UIKit/UIKit.h>

@interface DrawRect : UIView {

}
@end

.m--------------------------------------------------------------
#import "DrawRect.h"


@implementation DrawRect


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


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
    [super dealloc];
}

@end


When I run this code I get the warning:
warning: 'DrawRect' may not respond to '-setNeedsDisplay:'

And when I click the button I get this message in the debugger:
-[DrawRect setNeedsDisplay:]: unrecognized selector sent to instance 0x59111d0

I think it has to do with the fact that I'm not using "initWithFrame" when I instantiate the DrawRect class, but I'm lost on how this is done or if I need it.

Thanks,
waffe





What is this:
  DrawRect *myRectView = [DrawRect alloc];

I thought you have a view and you need to redraw it. In this case you use setNeedsDisplay:
[theView setNeedsDisplay:YES];

You use a strange name: DrawRect. It is a class derived from UIView. But you create it in strange way: just call alloc method. This code is wrong. (I think, you wanted to create a view controller.)
If you use UIView you can create it with alloc and initWithFrame methods, for example:
UIView *myView = [[UIView alloc] initWithFrame:myRect];

ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check also here:
Create UIView, UILabel and UIButton programmatically.
http://www.wmdeveloper.com/2010/07/create-uiview-uilabel-and-uibutton.html
Avatar of waffe

ASKER

Thanks pgnatyuk,

I'm going to look over the links you posted. I'm new to iPhone programming and this program is my first attempt at getting something of my own working. Drawing to a view view buttons seemed like an OK place to start.

Would a better name for drawRect be "rectView"?

And what is myRect in
UIView *myView = [[UIView alloc] initWithFrame:myRect];

maybe after a few more tuts i'll understand a bit better.

thanks,
4D
myView - that's the name of the UIView object.
I wish you good luck. Use the links I posted. Don't be in hurry and do not take long tutorials and programs for the beginning. You have enough to learn with one view.
Avatar of waffe

ASKER

Got it!!!

The first tutorial you posted for me was fantastic! I had done a few already but removing IB for a moment was just what I needed - more like what I'm used to.

I then did this parallel tutorial with IB:
http://www.switchonthecode.com/tutorials/creating-your-first-iphone-application-with-interface-builder

Thanks again,
waffe
You are welcome.