2011-02-10 90 views
0

晚上好, 這是問題,[self.view addSubview:pauseView]; 在將pauseView加載到當前視圖之前BOOL isPaused被轉換爲false,然後出現子視圖。我試圖將該變量的值由於使用暫停視圖而改爲false,但由於它不在當前類上,所以我無法執行此操作。 我知道這個話題已經覆蓋在計算器上,但我仍然無法解決我的問題。如果我能解決這個問題,它將解決我的另外3個應用程序中的同樣的問題。訪問控制器變量或方法從視圖子視圖類

真誠, Sonic555gr

回答

0

定義isPaused得到如在定義isPaused得到的類的屬性(我們稱之爲馬西德威):


// inside MasterView.h 
@property (nonatomic,assign) BOOL isPaused;

然後讓你的子視圖pauseView自定義UIView子類(姑且稱之爲PauseView)並在這個子類中定義了一個名爲MasterView的屬性:MasterView:


// inside PauseView.h 
@property (nonatomic,assign) MasterView *master

然後當你alloc/init喲烏爾pauseView只是設置該屬性:


// somewhere inside MasterView.m 
PauseView *pauseView = [[PauseView alloc] initWithFrame:frame]; 
pauseView.master=self; 

最後,在你的PauseView類,在你想改變isPaused得到屬性代碼的角度來看,這樣做:


// somewhere in PauseView.m 
master.isPaused=YES
+0

現在我明白,如果我傳遞給它的子視圖之一,我的主控制器的一個實例的引用,然後我可以操控任何變量!你清理了我的頭,爲此我感謝你! :D – 2011-02-11 06:17:56

0

你真的應該有一個考慮一下你的架構,並嘗試將你的應用程序邏輯從UIViews轉移回控制器(即代表可能是一個不錯的選擇,但不可能知道沒有看到更多的代碼和你想要達到的目標)。

如果你堅持操縱UIView中的變量,你需要在初始化時將你的viewController的引用傳遞給pauseView。

因此,在你PauseView類,你可以創建一個自定義的初始化器:

-(id)initWithFrame:(CGRect)frame andViewController:(id)vc { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Any other custom initialisation here 
    } 
} 
相關問題