帮助理解UIVIewController和UIView子类之间的交互

时间:2010-12-09 01:02:57

标签: iphone ios interface-builder

这是目前的情况:

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

然后在Interface Builder中我创建了一个90x115的UIView,并将它的Class Identity设置为“draw”。 当我运行应用程序(没有两个非编译行)时,它会在屏幕中间显示一个漂亮的红色矩形。我的目标是能够从TestViewController中更改矩形的颜色。但是,当我编译测试应用程序时,我得到以下编译器错误:

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'

我知道我错过了我的TestViewController和draw之间的“链接”,但无法弄清楚如何实现它。我尝试了各种技巧,但没有任何效果。 有人可以解释为了

需要做些什么
draw.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1].CGColor;
[draw setNeedsDisplay];

编译和工作? (我会用这个知识在TestViewController中创建一个不同的方法,我只是在viewDidLoad中测试它)

3 个答案:

答案 0 :(得分:1)

在你发布的代码中,你设置了rectColor属性并在draw上调用setNeedsDisplay:但是draw是你的类。您需要对实例执行此操作。您不需要为此创建新属性,因为UIViewController定义了view属性。只需在viewDidLoad方法中使用self.view代替draw即可。另外,删除该行末尾的.CGColor

其次,drawRect:方法忽略该颜色并使用它自己的颜色。变化

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

CGColorRef myColor = self.rectColor.CGColor;

答案 1 :(得分:0)

这里的一个大问题是你没有为draw宣布一个属性;如果要访问它,必须执行此操作并将其链接到Interface Builder对象。解决方案是创建一个包含draw对象的属性,并使用@synthesize合成其setter和getter。 (最后一部分就是你收到错误的原因。)

将代码更改为我在下面的代码后,您需要在Interface Builder中进行正确的连接,否则您会发现代码更改根本没有效果,即使它们没有错误。

大写你的班级名称(总是!),然后试试这个:

TestViewController.h:

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

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

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

然后,对于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

我认为应该这样做。

答案 2 :(得分:0)

你定义UIColor* rectColor;,但你也写了一个CGColor

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

尝试

self.drawCopy.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1];
相关问题