2010-12-09 49 views
0

這裏的現狀:幫助理解互動

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中測試它)

+0

哎,我拿出了應用程序委託的東西,因爲它使得這個問題很長,而且絕對沒有連接到這個錯誤。希望這可以幫助! – 2010-12-09 01:48:41

+0

謝謝。我不確定,這就是爲什麼我把它放在那裏。 – TrekOnTV2017 2010-12-09 01:58:53

回答

1

在您發佈的代碼,可以設置rectColor屬性並調用setNeedsDisplay:在平局,但平局你的班。您需要對該實例執行此操作。你不需要爲此創建一個新的屬性,因爲UIViewController定義了視圖屬性。只需在您的viewDidLoad方法中使用self.view而不是draw。此外,刪除線末尾的.CGColor

其次,drawRect:方法會忽略該顏色並使用它自己的顏色。更改

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

CGColorRef myColor = self.rectColor.CGColor; 
0

這裏的一個大問題是,你還沒有爲draw聲明一個屬性;如果要訪問它,必須執行此操作並將其鏈接到Interface Builder對象。解決方案是創建一個屬性,該屬性將保存draw對象,並使用@synthesize合成其設置者和獲取者。 (即最後一塊就是爲什麼你得到的錯誤。)

一旦修改代碼,我有什麼下面,你需要在界面生成器正確連接,否則你會發現你的代碼更改根本沒有效果,即使它們沒有錯誤。

大寫的類名(永遠!),然後試試這個:

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 

我認爲,應該做的伎倆。

+0

我想我已經做了修改,但我仍然得到一個錯誤........警告:從不兼容的指針類型傳遞'setRectColor:'的參數1 .......這是在自我.drawCopy.rectColor = [UIColor colorWithHue:0 saturation:1 brightness:0.9 alpha:1] .CGColor; – TrekOnTV2017 2010-12-09 02:05:05

+0

啊,哎呀 - 嘗試從最後刪除`.CGColor`,所以你只是給屬性分配一個'UIColor`。我會改變我的代碼以反映這一點! – 2010-12-09 02:12:53

0

你definde 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];