Objective c 如何从UIViewController调用UIView子类方法?
在我的Objective c 如何从UIViewController调用UIView子类方法?,objective-c,uiview,uiviewcontroller,Objective C,Uiview,Uiviewcontroller,在我的hyplosisviewcontroller.m中,我有一段代码,用于将UIView子类hyplosisview添加到窗口中。我的目标是在UISegmented控件更改其值时,设置MyHyposisView实例的属性UIColor circleColor - (void) loadView { CGRect frame = [[UIScreen mainScreen] bounds]; HypnosisView *v = [[HypnosisView alloc] ini
hyplosisviewcontroller.m
中,我有一段代码,用于将UIView
子类hyplosisview
添加到窗口中。我的目标是在UISegmented
控件更改其值时,设置MyHyposisView
实例的属性UIColor circleColor
- (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];
}
然后,我想从这里开始使用iAction
出口,但在使用此代码时,xcode无法识别自定义类中的getter/setter方法:
- (IBAction)setRingColor:(id)sender
{
if ([sender selectedSegmentIndex] == 0)
{
[self.view setCircleColor:[UIColor redColor]];
}
}
我如何将其传达给我的自定义
UIView
?您必须将其向下转换为其派生类型
[((HypnosisView *)self.view) setCircleColor:[UIColor redColor]];
此外,只要该控制器的所有实例的视图类始终相同,您可能希望编写一个类型细化类别,以将
视图
属性重新定义为具有更特定的类型。这有助于避开恶劣的演员阵容。