2012-02-04 46 views
0

這是我的代碼,可以在我的主窗口中加載兩個不同的自定義視圖。我的問題是,當我點擊一個按鈕來調用我的PageTwo方法時,我要求它從PageOne中刪除當前的視圖,並將其替換爲新的「text1Title2」。問題是,這將無法正常工作,除非我重構我的代碼並將其放在一個方法中,在這種情況下,我需要一個switch語句,我不知道如何執行此操作。有沒有辦法從另一個方法中刪除視圖,或者當PageTwo方法被調用時釋放該方法?如何從不同的方法中刪除視圖

- (void) PageOne 
    { 
     NSLog(@"FirstPart"); 

     WelcomeScreenText1* text1ViewController = 
     [[WelcomeScreenText1 alloc] initWithNibName:text1Title bundle:nil]; 
     if (text1ViewController != nil) 
     { 
      myCurrentViewController = text1ViewController; 
     } 

     // embed the current view to our host view 
     [myTargetView addSubview: [myCurrentViewController view]]; 

     // make sure we automatically resize the controller's view to the current window size 
     [[myCurrentViewController view] setFrame: [myTargetView bounds]]; 
    } 

    - (void) PageTwo 
    { 

     if ([myCurrentViewController view] != nil) 
      [[myCurrentViewController view] removeFromSuperview]; // remove the current view 

     if (myCurrentViewController != nil) 
      [myCurrentViewController release]; 

     WelcomeScreenText2* text2ViewController = 
     [[WelcomeScreenText2 alloc] initWithNibName:text1Title2 bundle:nil]; 
     if (text2ViewController != nil) 
     { 
      myCurrentViewController = text2ViewController; 
     } 

     // embed the current view to our host view 
     [myTargetView addSubview: [myCurrentViewController view]]; 

     // make sure we automatically resize the controller's view to the current window size 
     [[myCurrentViewController view] setFrame: [myTargetView bounds]]; 

    } 

回答

0

您可以簡單地使用UIViews(而不是UIViewControllers)。將它們作爲子視圖添加到主視圖中,並根據需要隱藏它們。

-(void)switch { 
    textView1.hidden = !textView1.hidden; 
    textView2.hidden = !textView2.hidden; 
}