Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

drawRect inside of a UIViewControll

Posted on 2010-08-25
9
Medium Priority
?
1,234 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Accepted Solution

by:
pgnatyuk earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Steve Jobs once said that Blu-ray is "a bag of hurt". As Macs users well known, things haven’t settled down (at least not from Apple’s perspective). Several years after that comment, Mac OS X still doesn’t support Blu-ray playback, nor has any Mac s…
A lot of new and distinct gadgets are making their appearance every other day. The latest gadget that has wooed the attention of all gadget lovers and non gadget lovers alike is the Smartwatch. This tiny gadget is capable of offering live access to …
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 how to create, access, and change arrays in the C programming language.

783 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