开发者

help understanding interaction between UIVIewController and UIView subclass

here's the current situation:

TestViewController.h:

#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController {
}
@end

TestViewController.m:

#import "TestViewController.h"
#import "draw.h"
@implementation TestViewController

 - (void)viewDidLoad {
draw.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1].CGColor;
[draw setNeedsDisplay];
[super viewDidLoad];
}

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

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

draw.h:

#import <UIKit/UIKit.h>

@interface draw : UIView {
UIColor* rectColor;
}

@property (retain) UIColor* rectColor;

@end

draw.m:

#import "draw.h"

@implementation draw

@synthesize rectColor;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (void)drawRect:(CGRect)rectangle {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorRef myColor = [UIColor colorWithHue:0 saturation:1 brightness:0.61 alpha:1].CGColor;
    CGContextSetFillColorWithColor(context, myColor);
    CGContextAddRect(context, CGRectMake(0, 0, 95.0, 110.0));
    CGContextFillPath(context);
}

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

@end

then in the Interface Builder I've created a 90x115 UIView and set it's Class Identity to "draw". When I run the application (without the two non-compiling lines) it displays a nice red rectangle in the middle of the screen. My goal is it be able to change the color of the rectangle from within my TestViewController. However when I compile the test app I get the following compiler errors:

error: accessing unknown 'setRectColor:' class method
error: object cannot be set - either readonly property or no setter found
warning: 'draw' may not respond to '+setNeedsDisplay'

I know I am missing "a link" between my TestViewController and draw, but can't figure out how to go about implementing it. I have tried various tricks, but nothing worked. Could someone please explain what needs to be done in order for

draw.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:开发者_开发百科0.6 alpha:1].CGColor;
[draw setNeedsDisplay];

to compile and work ? (I would use this knowledge to create a different method in TestViewController, I am just testing it inside viewDidLoad)


In the code you posted, you set the rectColor property and call setNeedsDisplay: on draw, but draw is your class. You need to do it to the instance. You shouldn't need to create a new property for this because UIViewController defines the view property. Just use self.view instead of draw in your viewDidLoad method. Also, remove the .CGColor at the end of the line.

Second, your drawRect: method ignores that color and uses its own. Change

CGColorRef myColor = [UIColor colorWithHue:0 saturation:1 brightness:0.61 alpha:1].CGColor;

to

CGColorRef myColor = self.rectColor.CGColor;


The big problem here is that you haven't declared a property for draw; you have to do this and link it up to your Interface Builder object if you want to access it. The solution is to create a property that will hold the draw object, and synthesize its setters and getters using @synthesize. (that last piece is why you're getting the error.)

Once you change the code to what I have below, you'll need to make the proper connection in Interface Builder, or else you'll find that your code changes simply have no effect, even though they're error free.

Capitalize your class names (always!), and then try this:

TestViewController.h:

#import <UIKit/UIKit.h>
@class Draw; //forward class declaration

@interface TestViewController : UIViewController {
    IBOutlet Draw *drawCopy;
}

@property (nonatomic, retain) Draw *drawCopy;
@end

Then, for TestViewController.m:

#import "TestViewController.h"
#import "Draw.h"
@implementation TestViewController

@synthesize drawCopy;

-(void) viewDidLoad {
    self.drawCopy.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1];
    [self.drawCopy setNeedsDisplay];
    [super viewDidLoad];
}

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

@end

I think that should do the trick.


you definde UIColor* rectColor; but u write an CGColor too it

draw.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1].CGColor;

try

self.drawCopy.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜