Solved

drawRect inside of a UIViewControll

Posted on 2010-08-25
9
1,227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arduino EDI - Programming language 3 128
Apple licensing for business 3 85
Mac monitoring software required for hardware and services, RMM type 4 77
Windows 10 14 40
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

710 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