2011-07-09 45 views
5

我敢肯定這是一個愚蠢的錯誤,但我想過去一小時從我的超級視圖中刪除子視圖沒有任何成功。removeFromSuperview導致我的應用程序崩潰

在我的第一個觀點我遇到

UIViewController *helpView = [[[UIViewController alloc] initWithNibName:@"HelpView" bundle:nil] autorelease]; 
[self.view addSubview:helpView.view]; 

然後裏面helpView我有一個連接到名爲「closeHelp」的IBAction爲剛剛執行以下操作按鈕:

- (IBAction) closeHelp{ 
    [self.view removeFromSuperview]; 
} 

但是這會導致我的應用程序因EXC_BAS_ACCESS而出現一些奇怪的原因,即使是那些在HelpView中,也就是說self.view應該指向正確的子視圖。

Wo üld感謝您的幫助

謝謝。
Shai。

+0

在什麼課你有'-closeHelp'?它是否在界面構建器中正確連接?您是否已將HelpView.xib中的類設置爲該類? – Eiko

回答

5

正如Andreas所回答的那樣,您試圖從超級/父級視圖中刪除self.view。 您基本上需要從其父視圖中刪除helpView。

所以它應該是

- (IBAction) closeHelp{ 
    [helpView removeFromSuperview]; 
} 

但我們不知道什麼是「helpView」在上面的方法。因爲我們沒有任何處理它。

所以我們的代碼應該看起來像這樣。

#define HELP_VIEW_TAG 101 // Give tag of your choice 

HelpView *helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil]; 
helpView.view.tag = HELP_VIEW_TAG; 
[self.view addSubview:helpView.view]; 
[helpView release]; 

- (IBAction) closeHelp{ 
    UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG]; 
    [helpView removeFromSuperview]; 
} 
+0

嘿,謝謝!我試圖這樣做的解決方案,但現在的子視圖甚至不會加載...(EXC_BAD_ACCESS) 的UIViewController * helpView = [[[UIViewController中的alloc] initWithNibName:@ 「HelpView」 束:無]自動釋放]; helpView.view.tag = HELP_VIEW_TAG; [self.view addSubview:helpView.view]; [helpView release]; –

+0

好現在它顯示(沒有與發佈一些愚蠢的事),但關閉按鈕仍然無法工作...... 的UIView * helpView = [self.view viewWithTag:101] [helpView removeFromSuperview]; (沒有硬編碼,以防萬一在例如標籤號) –

+0

我希望你的幫助視圖獲取添加爲子視圖,並顯示。我想象你在幫助視圖上有一個關閉按鈕,並且它與上面的代碼中的操作方法相關聯,這應該消除自己,是嗎? – 2011-07-09 08:50:05

2

self.view並不指向您的子視圖,而是您的uiviewcontroller管理的根視圖。您應該只刪除子視圖堆棧中的最後一個對象,而不是整個視圖,因爲現在您要刪除整個幫助視圖。

無論如何,你爲什麼不以模態方式呈現視圖控制器而不是這樣做?

[self presentModalViewController:helpView animated:NO/YES]; 

helpView. modalTransitionStyle = //One of the constants below 

UIModalTransitionStyleCoverVertical 
UIModalTransitionStyleFlipHorizontal 
UIModalTransitionStyleCrossDissolve 
UIModalTransitionStylePartialCurl 

通常我,這將在模態呈現視圖 - 控制寫self.modalTransitionStyle = // One of the constants ,而非四處傳播的代碼。

+0

無論如何,這個方法屬於HelpViewController,最後一句在這裏沒有意義。如果您想修改視圖層次結構,則無論如何都需要加載視圖,而self.view是引用視圖的適當方式。 – Eiko

1

您正在初始化helpView作爲UIViewController
確保在您初始化視圖控制器的.h文件中有#import "HelpView.h"(或任何helpView .h文件被調用)。

然後,使用此代碼:

HelpView *helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil]; 
[self.view addSubview:helpView.view]; 

這應該修復它。

+0

他基本上是在代碼中,試圖從superview中刪除self.view,這可能應該是零。 – 2011-07-09 08:27:27

+0

這可能是正確的答案。 – Abizern

-1
Declare the help view on calss level. 

in.h file 

@class HelpView; 
.. 
@interface 
{ 
HelpView *helpView; 
} 
@property(nonatomic,retain)HelpView* helpView; 


In.m file 
#import "HelpView" 
@synthensize helpView; 



now add this Code where you want 

helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil]; 
helpView.view.tag = HELP_VIEW_TAG; 
[self.view addSubview:helpView.view]; 


- (IBAction) closeHelp{ 
    //UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG]; 
    [helpView removeFromSuperview]; 
} 

- (無效)的dealloc { [helpView釋放]; }

+0

在父dealloc或helpview dealloc中?我在猜測父母?此文件的.m文件中加入了 –

+0

add - (void)dealloc {[helpView release]} – dark

+0

添加代表視圖控制器沒有太多意義。它混淆了初學者; P – Greg

0

最簡單的解決方案,我最終是隻定義我的XIB的文件所有者爲同一類父控制器,這意味着父控制器將控制這兩個父和子視圖,這只是讓輕鬆了許多。:)