2013-03-27 55 views
0

我在一個xib中有一個CustomView,在兩個不同的xib中有兩個不同的視圖。 我想在一個CustomeView中一個接一個地顯示這兩個視圖。 我的NSView的目的,其被連接到在CustomView文件的.xib在一個自定義視圖中將一個視圖切換到另一個視圖

@property (retain) IBOutlet NSView *mySubview; 
@property (retain) NSViewController *viewController; 

方法來打開一個觀點是:

-(IBAction)selectBookTicket:(id)sender 
    { 
     //setting status label to nil 
     _viewController=[[NSViewController alloc] initWithNibName:@"BookTicket" bundle:nil]; 
     //loading bookTicket xib in custom view of NormalUserWindow 
     [_mySubview addSubview:[_viewController view]]; 
    } 

法打開另一個視圖相同的CustomView是:

-(IBAction)selectTicketCancellation:(id)sender 
    { 
     _viewController=[[NSViewController alloc] initWithNibName:@"CancelTicket" bundle:nil]; 
     //loading CancelTicket xib in custom view of NormalUserWindow 
     [_mySubview addSubview:[_viewController view]]; 
    } 

當我第一次打開任何視圖時,它在CustomView中正確顯示,但是當我嘗試第二次打開第二個視圖或同一個視圖時,它會在先前打開的視圖上重疊。

我試圖

[_mySubview removeFromSuperview]

它移除「mySubview」完全,我的意思是什麼都認爲當前加載它得到消除,但它不是允許顯示任何意見之後'[_mySubview removeFromSuperview]'得到執行。

回答

1

您只能刪除已從視圖控制器添加的視圖。改爲嘗試下面的代碼。

-(IBAction)selectTicketCancellation:(id)sender 
    { 
     [[_viewController view] removeFromSuperView]; 
     _viewController=[[NSViewController alloc] initWithNibName:@"CancelTicket" bundle:nil]; 
     //loading CancelTicket xib in custom view of NormalUserWindow 
     [_mySubview addSubview:[_viewController view]]; 
    } 

執行[_mySubview removeFromSuperview]將刪除宿主視圖(即,也就是說從其他視圖控制器顯示視圖的一個)從視圖層次結構,這解釋了「不允許顯示任何其他子視圖的一部分」。

相關問題