2009-10-03 34 views
3

後存在我添加的firstView在AppDelegate中:的UIView繼續removeFromSuperview

#import "TestViewAppDelegate.h" 
#import "MainViewController.h" 
@implementation TestViewAppDelegate 

@synthesize window; 
@synthesize mainViewController; 

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil]; 
    self.mainViewController = aController; 
    [aController release]; 

    self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame; 
    [window makeKeyAndVisible]; 
    [window addSubview:mainViewController.view]; 
} 

- (void)dealloc { 
    [mainViewController release]; 
    [window release]; 
    [super dealloc]; 
} 

然後我想切換到secondView:

#import "MainViewController.h" 
#import "SecondViewController.h" 

@implementation MainViewController 

@synthesize mainViewController, secondViewController; 

- (IBAction)viewSwitch 
{ 
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil]; 
    self.secondViewController = second; 
    [second release]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.75]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 

    [mainViewController.view removeFromSuperview]; 
    [self.view addSubview:secondViewController.view]; 

    [UIView commitAnimations]; 
} 

- (void)dealloc { 
    [mainViewController release]; 
    [secondViewController release]; 
    [super dealloc]; 
} 

@end 

然後同樣的事情從secondView切換到firstView ...

問題是,當我切換視圖,我認爲釋放總是可見的,不會消失。

Download the full code

回答

1

更好的方法可能是創建一個添加到窗口的頂級父視圖控制器。它應該有一個對其他視圖控制器的引用,並進行交換。如果你想讓每個視圖中的按鈕導致交換,那麼你可以在每個帖子的IBAction按鈕上添加一個通知,告知頂層視圖註冊並響應。

+0

感謝您的回覆。 我也會嘗試這種方式,但需要更多的幫助。如果我將視圖控制器添加到IB的窗口中,是否還需要在MainWindow.xib中添加一個視圖,該視圖將成爲頂層視圖? – Rufilix

+0

最後我遵循了這個方法。 – Rufilix

+0

你有沒有找到你的問題的答案?這給出了一個不同的方法,但似乎並沒有直接回答什麼是不工作的。我試圖找出一個類似於你的問題。 – Jacob

2
[mainViewController.view removeFromSuperview]; 
[self.view addSubview:secondViewController.view]; 

什麼是 「自我」 在這裏?爲什麼MainViewController類有一個名爲mainViewController的屬性?探索這些問題可能會使你在這裏得到答案。

+0

感謝您的回覆。 我需要在主屏幕上添加secondView.xib,這就是爲什麼我要求self.view添加。這是錯的嗎 ? 屬性mainViewController已創建,用於刪除與使用的MainViewController關聯的firstView.xib。因爲我無法刪除沒有屬性的xib。 我沒有找到答案,但仍在探索... – Rufilix