Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-24-2009, 04:19 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 27
Default drawRect not called

Hi all

Very new to iphone developement, but have 10yrs as a java developer.
I have two questions about a project of mine.

First:

Trying to draw some lines on the screen.

I have a view controller in which I try to trigger the drawRect method like this:

Code:
- (void)loadView {
	NSLog(@"chartView loading");
	chartView = [[ChartView alloc] initWithFrame:CGRectMake(1.0, 0.0, 1.0, 1.0)];
	[chartView setNeedsDisplay];
the view looks like this:
Code:
#import "ChartView.h"

#import <UIKit/UIKit.h>


@implementation ChartView


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

-(void)drawRect:(CGRect)
 {
	NSLog(@"in drawRect");
    // Drawing code
	// CGContextRef context = UIGraphicsGetCurrentContext();
	 CGRect box = CGRectMake(40, 40, 240, 120);
	 CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
	 CGContextFillRect(context, box);
	 
	 CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
	 CGContextStrokeRectWithWidth(context, box, 10);
	 
	
}


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


@end
I see printouts of the "initWithFrame" methode being called but not the drawRect. Shouldn't drawRect be triggerd for the setNeedsDispaly call?


Question nr 2.

How do i get my view "chartView" into the IB? I read that you are supposed to be able to drag it into the window with "File Owner" and that stuff, and then it should be available. I see the green + sign when I drag it there but it doesnt show up anywhere i the library window. I want to make that view smaller and fit in on the view togheter with a few textinputs and buttons.


Thanks for your time.

/x

Last edited by xworker; 03-24-2009 at 06:54 AM.
xworker is offline   Reply With Quote
Old 03-24-2009, 08:17 AM   #2 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Quote:
Originally Posted by xworker View Post
Hi all

Very new to iphone developement, but have 10yrs as a java developer.
I have two questions about a project of mine.

First:

Trying to draw some lines on the screen.

I have a view controller in which I try to trigger the drawRect method like this:

Code:
- (void)loadView {
	NSLog(@"chartView loading");
	chartView = [[ChartView alloc] initWithFrame:CGRectMake(1.0, 0.0, 1.0, 1.0)];
	[chartView setNeedsDisplay];
the view looks like this:
Code:
#import "ChartView.h"

#import <UIKit/UIKit.h>


@implementation ChartView


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

-(void)drawRect:(CGRect)
 {
	NSLog(@"in drawRect");
    // Drawing code
	// CGContextRef context = UIGraphicsGetCurrentContext();
	 CGRect box = CGRectMake(40, 40, 240, 120);
	 CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
	 CGContextFillRect(context, box);
	 
	 CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
	 CGContextStrokeRectWithWidth(context, box, 10);
	 
	
}


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


@end
I see printouts of the "initWithFrame" methode being called but not the drawRect. Shouldn't drawRect be triggerd for the setNeedsDispaly call?


Question nr 2.

How do i get my view "chartView" into the IB? I read that you are supposed to be able to drag it into the window with "File Owner" and that stuff, and then it should be available. I see the green + sign when I drag it there but it doesnt show up anywhere i the library window. I want to make that view smaller and fit in on the view togheter with a few textinputs and buttons.


Thanks for your time.

/x

drawRect is send to the DELEGATE of the view. Did you set chartview.delegate?

Also, why did you create the view to be only 1 pixel wide and high?

Do you use a ViewController? Then the .view property of it needs to point to your view as well.

Question 2: Just create a new UIView from the Library and put it where you want the view to be. Then go to the rightmost tab in the inspector and change the class to be ChartView.

cheers
Oliver


Dr. Touch

Last edited by Oliver Drobnik; 03-24-2009 at 08:25 AM.
Oliver Drobnik is offline   Reply With Quote
Old 03-24-2009, 08:31 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 27
Default

Thanks for your answer.

How do i set the delegate? Can you show me? I thought a controller and a delegate was the same thing...

thanks for your time.

/x
xworker is offline   Reply With Quote
Old 03-24-2009, 08:49 AM   #4 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Quote:
Originally Posted by xworker View Post
Thanks for your answer.

How do i set the delegate? Can you show me? I thought a controller and a delegate was the same thing...

thanks for your time.

/x

set delegate like this:


GraphView *myGraphView; // variable to hold pointer, could be in header of viewcontroller
myGraphView.delegate = self; // sets it to where you are currently

you don't need to set it to itself though, I think. Usually you would have delegate functions in the viewcontroller.

A Viewcontroller has some things that a view itself cannot do, like being pushed ontop of a navigationcontroller. Check the docs: UIViewController versus UIView. You very rarely need to subclass UIView only if you have very special drawing logic.

Unfortunately my MBP is being serviced as we speak, otherwise I would have offered that you send me the source and I find the problem for you.

cheers
Oliver

Dr. Touch
Oliver Drobnik is offline   Reply With Quote
Old 03-24-2009, 08:57 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 27
Default

Quote:
Originally Posted by Oliver Drobnik View Post
set delegate like this:


GraphView *myGraphView; // variable to hold pointer, could be in header of viewcontroller
myGraphView.delegate = self; // sets it to where you are currently

you don't need to set it to itself though, I think. Usually you would have delegate functions in the viewcontroller.

A Viewcontroller has some things that a view itself cannot do, like being pushed ontop of a navigationcontroller. Check the docs: UIViewController versus UIView. You very rarely need to subclass UIView only if you have very special drawing logic.

Unfortunately my MBP is being serviced as we speak, otherwise I would have offered that you send me the source and I find the problem for you.

cheers
Oliver

Dr. Touch
Thanks!

I did set the view in my controller header file, but not the self thing.
Looks like this:

Code:
#import <UIKit/UIKit.h>
#import "ChartView.h"

@interface WeightApp2ViewController : UIViewController {

	IBOutlet UITextField *txtWeight;
	IBOutlet UITextField *txtHeight;
	IBOutlet UILabel *lblBmi;
	ChartView *chartView;
}

@property(nonatomic,retain) IBOutlet UITextField *txtWeight;
@property(nonatomic,retain) IBOutlet UITextField *txtHeight;
@property(nonatomic,retain) IBOutlet UILabel *lblBmi;

- (IBAction) calculateBmi:(id) sender;

@end
and the implementation of the controller:

Code:
#import "WeightApp2ViewController.h"

@implementation WeightApp2ViewController

@synthesize txtWeight, txtWeight, lblBmi;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}



- (void)loadView {
	NSLog(@"chartView loading");
	chartView = [[ChartView alloc] initWithFrame:CGRectMake(1.0, 0.0, 1.0, 1.0)];
	[chartView setNeedsDisplay];

	
}
Gona test your solution when I get home from work

/x
xworker is offline   Reply With Quote
Old 03-24-2009, 12:15 PM   #6 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Did you add your view to the view hierarchy? It can't draw unless its a subview of something.
PhoneyDeveloper is offline   Reply With Quote
Old 03-24-2009, 04:53 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 27
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
Did you add your view to the view hierarchy? It can't draw unless its a subview of something.
I have added like this :


#import "WeightApp2AppDelegate.h"
#import "WeightApp2ViewController.h"

@implementation WeightApp2AppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunchingUIApplication *)application {

[window addSubview:viewController.view];
[window makeKeyAndVisible];
}


- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}


@end

/x

Last edited by xworker; 03-24-2009 at 05:07 PM.
xworker is offline   Reply With Quote
Old 03-24-2009, 05:29 PM   #8 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Remove your loadView method. Add viewDidLoad and build your chart view in viewDidLoad. Add this to viewDidLoad:

Code:
[self.view addSubView: chartView];
remove setNeedsDisplay.

also you need your chart view to be bigger than 1x1.
PhoneyDeveloper is offline   Reply With Quote
Old 03-24-2009, 05:30 PM   #9 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
Remove your loadView method. Add viewDidLoad and build your chart view in viewDidLoad. Add this to viewDidLoad:

Code:
[self.view addSubView: chartView];
remove setNeedsDisplay.

also you need your chart view to be bigger than 1x1.
I'm also not clear why you don't just add the chart view in IB if you're building a nib anyway.
PhoneyDeveloper is offline   Reply With Quote
Old 03-24-2009, 05:41 PM   #10 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 27
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
I'm also not clear why you don't just add the chart view in IB if you're building a nib anyway.
That was the solution... Now it works. Added the view in IB and now it draws as it should.
xworker is offline   Reply With Quote
Reply

Bookmarks

Tags
controller, drawrect, interfacebuilder, view

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 245
18 members and 227 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, iDifferent, iph_s, JamesCahall, JasonR, mer10, Monstertaco, prchn4christ, Robiwan, Rudy, smithdale87, Speed, spiderguy84, timle8n1
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0