2012-07-21 53 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

相关问题