這裏的現狀:幫助理解互動
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
然後在界面生成器中我已經創建了一個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.rectColor = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1].CGColor;
[draw setNeedsDisplay];
編制和工作是什麼? (我會用這個知識在TestViewController中創建一個不同的方法,我只是在viewDidLoad中測試它)
哎,我拿出了應用程序委託的東西,因爲它使得這個問題很長,而且絕對沒有連接到這個錯誤。希望這可以幫助! – 2010-12-09 01:48:41
謝謝。我不確定,這就是爲什麼我把它放在那裏。 – TrekOnTV2017 2010-12-09 01:58:53