0

I am having of one CustomView in one xib and two different views in two different xib's. I want to display those two view one after the other in one single CustomeView. I have an object of NSView which is connected to CustomView in .xib file

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

Method to open one View is:

 -(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]];
  }

Method to open another view in same CustomView is:

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

When I am opening any view for first time its displaying properly in CustomView, but when I am trying to open second view or same view for second time then its get overlapping on previous opened view.

I tried

[_mySubview removeFromSuperview]

It's removing 'mySubview' completely, I mean what ever the view is currently loaded it is get removing but it's not allowing to display any views after that '[_mySubview removeFromSuperview]' get executed.

j0k
  • 22,600
  • 28
  • 79
  • 90
KrishnaTeja
  • 117
  • 2
  • 11

1 Answers1

1

You must remove only the view that you have added from the view controller. Try the following code instead.

-(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]];
  }

Executing [_mySubview removeFromSuperview] will remove your host view (i.e; the one that is displaying views from other view controllers) from view hierarchy and this explains the "not allowing to display any other subviews part".

Suhas
  • 1,500
  • 11
  • 15