2012-07-21 40 views
0

在我的HypnosisViewController.m我有這個代碼添加UIView子類,HypnosisView到窗口。我的目標是在UISegmented控件更改其值時設置HypnosisView實例的屬性UIColor circleColor如何從UIViewController調用UIView子類方法?

- (void) loadView 
{ 
    CGRect frame = [[UIScreen mainScreen] bounds]; 
    HypnosisView *v = [[HypnosisView alloc] initWithFrame:frame]; 
    CGRect segment = CGRectMake(200, 300, 75, 20); 
    UISegmentedControl *colors = [[UISegmentedControl alloc]initWithFrame:segment]; 
    [v addSubview:colors]; 
    [self setView:v]; 
} 

然後我就從這裏想以一個IBAction口,因爲這樣,但使用此代碼時的Xcode不承認我的getter/setter方法在我的自定義類:

- (IBAction)setRingColor:(id)sender 
{ 
    if ([sender selectedSegmentIndex] == 0) 
    { 
     [self.view setCircleColor:[UIColor redColor]]; 
    } 
} 

我怎麼能將此傳達給我的自定義UIView

回答

2

您必須將其向下轉換爲其派生類型。

[((HypnosisView *)self.view) setCircleColor:[UIColor redColor]]; 
+0

感謝 偉大的答案 – 2012-07-21 00:46:11

+2

此外,只要該視圖的班上總是會對該控制器的所有實例一樣,你不妨寫一個類型細化類別重新定義了'view'財產具有更具體的類型。這有助於避免糟糕的投射。 – 2012-07-21 03:36:09

相關問題