繁体   English   中英

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

[英]help understanding interaction between UIVIewController and UIView subclass

这是当前情况:

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,并将其类标识设置为“绘制”。 当我运行该应用程序时(没有两条非编译行),它在屏幕中间显示一个漂亮的红色矩形。 我的目标是能够从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中对其进行测试)

在发布的代码中,设置rectColor属性并在绘制时调用setNeedsDisplay:,但是draw是您的类。 您需要对实例进行操作。 您不需要为此创建新属性,因为UIViewController定义了view属性。 只需使用self.view而不是在viewDidLoad方法中draw 同样,删除该行末尾的.CGColor

其次,您的drawRect:方法将忽略该颜色并使用其自己的颜色。 更改

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

CGColorRef myColor = self.rectColor.CGColor;

这里最大的问题是您尚未声明draw属性。 如果您要访问它,则必须执行此操作并将其链接到Interface Builder对象。 解决方案是创建一个将保存draw对象的属性,并使用@synthesize合成其设置方法和获取方法。 (最后一部分是为什么您会收到错误消息。)

将代码更改为下面的代码后,您需要在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

我认为应该可以解决问题。

您定义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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM