[英]Accessing subview variables from another subview
我有一个带有两个子UIViews的UIViewController。 在第一个UIView中,我创建了一个绿色背景的UIView。 按下UIViewController中的按钮后,将其更改为红色背景。
现在我想将其更改为紫色背景,但是由于我在UIViewController中更改了背景,所以我无法再次更改它。
UIViewA.h
@interface UIViewA : UIView
@property UIView *colorView;
UIViewA.m
...
colorView = [[UIView alloc]init];
[colorView setBackgroundColor:[UIColor greenColor]];
...
UIViewController.h
#import "UIViewA.h"
#import "UIViewB.h"
@interface MainViewController : UIViewController
@property UIViewA *viewA;
@property UIViewB *viewB;
UIViewController.m
...
viewA = [[UIViewA alloc]init];
viewB = [[UIViewB alloc]init];
...
-(void)changeColor
{
[[viewA colorView] setBackgroundColor:[UIColor redColor]];
}
UIViewB.h
#import "UIViewA.h"
@interface UIViewB : UIView
@property UIViewA *ViewA;
UIViewB.m
...
ViewA = [[UIViewA alloc]init];
[[ViewA colorView] setBackgroundColor:[UIColor greenColor]];
// ^^ That rule doesn't seem to work. No errors were given.
...
编辑:我正在尝试做的简短示例。
如果我理解正确,您想在viewB中更改viewA的背景颜色。 您可以执行的操作是在viewcontroller.m中,在创建viewB之后,分配viewB.viewA = self.viewA
。 在viewB.m中,不要创建新的viewA,而应使用属性self.viewA,这样就可以更改viewA的背景颜色。 在viewB.m
[[self.viewA colorView] setBackgroundColor:[UIColor greenColor]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.