Solved

drawRect inside of a UIViewControll

Posted on 2010-08-25
9
1,222 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
ID: 33524958
You need to call setNeedsDisplay and drawRect will be called automatically.
[yourView setNeedsDisplay:YES];
0
 

Author Comment

by:waffe
ID: 33525836
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
ID: 33526108
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33526135
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
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33526150
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
ID: 33526656
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
ID: 33529448
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
ID: 33533222
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
ID: 33533753
You are welcome.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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:   …
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 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