Search code examples
iossubviewsuicontainerview

iOS: Programmatically calling Container View


Before the Main View Controller is called, through a delegate I'll notify Main View Controller whether to call TVC1 or TVC2 in Container View.

Question: How can I programmatically tell the Container View in Main View Controller to call TVC1 or TVC2?

TVC1 *tvc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"TVC1"]; 
TVC2 *tvc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"TVC2"];

Where and how do I tell the Container View to load one of these controllers upon loading Main View Controller?

enter image description here


Solution

  • through delegate u can know which view will be added so u can manage it by using any bool value n in viewDidLoad you can load that view by putting one condition

    in mainController:

    bool isFirstView;
    
    -(void)delegate:(bool)isFirst
    {
      isFirstView = isFirst;
    }
    
    // in viewDidLoad 
    )
    (void)viewDidLoad
    {
      if(isFirstView)
      {
        TVC1 *tvc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"TVC1"]; 
      }
      else
      {
        TVC2 *tvc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"TVC2"];
      }
    }