2011-07-19 41 views
0

有沒有辦法檢查UIViewController是否加載到內存中/在屏幕上可見?Cocoa-Touch:在初始化之前檢查UIViewController是否存在

事情是這樣的:

if([ContentRvController exists]){ 
    contentView *ContentRvController = [[contentView alloc] 
     initWithNibName:@"contentView" bundle:nil]; //ContentView is a custom UIViewController 
    .... 
    //Code to set the UIViewController 
    .... 
} 
else{ 
    [ContentRvController release]; 
} 

當按鈕(即現在初始化ViewControllers)被竊聽應該發生的。現在,當點擊它時會打開n個ViewControllers,它應該一次只顯示一個。

這就是它,問候和希望,你可以幫助我。

回答

0

這是基於現有的代碼嗎? 類應以大寫字母開頭,實例應爲駱駝式,例如

if([contentRvController exists]){ 
    ContentView *contentRvController = [[ContentView alloc] 
    initWithNibName:@"contentView" bundle:nil]; //ContentView is a custom UIViewController 
    .... 
    //Code to set the UIViewController 
    .... 
} 
else{ 
    [contentRvController release]; 
} 

它可能是值得在頭宣佈它,即

@interface SomeClass : NSObject { 

} 
@property(non-atomic, retain) ContentView *contentRvController; 
@end 

,然後在代碼中,你可以做

if(contentRvController!=nil){ 
    ContentView *aView=[[[ContentView alloc] init] autorelease]; 
    self.contentRvController=aView; 
} 

另外,不要做其他{[contentRv釋放];}位,如果你有任何地方autoreleased它,這將在某個時候泄漏。

+0

這會做到這一點。 –

+0

只是一個小問題。我在那裏發佈的片段調用另一個類的ContentView。如果ContentView存在,隱藏視圖,將其設置爲零並釋放它,但ContentView本身有一個關閉按鈕,當點擊時,它也會執行相同的操作。 問題是,當在IBAction(ContentView內部)時,'self = nil;'實際上並沒有將創建的ContentView設置爲零(或者至少在執行if(contentRvController = nil)時,它將返回false ,所以我必須點擊兩次啓動按鈕才能工作。 –

0

如果您使用UINavigationController檢查屬性topViewController

+0

不,內容視圖添加了addSubView(也許有點奇怪,但由於UIView漂浮在模擬桌面上,這是我能找到的唯一方法,您可以拖動視圖和所有內容)。 –

相關問題