Solved

drawRect inside of a UIViewControll

Posted on 2010-08-25
9
1,219 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:waffe
  • 6
  • 3
9 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
You need to call setNeedsDisplay and drawRect will be called automatically.
[yourView setNeedsDisplay:YES];
0
 

Author Comment

by:waffe
Comment Utility
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





0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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];

0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
Comment Utility
Check if this tutorial will explain the subject for you:
http://www.switchonthecode.com/tutorials/an-absolute-beginners-guide-to-iphone-development

Or maybe this one: http://www.roseindia.net/tutorial/iphone/examples/creating-UIView.html

I do not know your program, seems like you just test something.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
Check also here:
Create UIView, UILabel and UIButton programmatically.
http://www.wmdeveloper.com/2010/07/create-uiview-uilabel-and-uibutton.html
0
 

Author Comment

by:waffe
Comment Utility
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
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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.
0
 

Author Comment

by:waffe
Comment Utility
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
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
You are welcome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now