[英]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.