Search code examples
iosxcodeuitabbarcontrollersegue

Select UITabbarItem from embedded Container UIViewController


How can I select programmatically a UITabbarController item from a UISegment from other ViewController?

ViewcontrollerA has a UISegmentControl and a ContainerView

UITabbarControllerB is embedded in the ContainerView from ViewControllerA and has itself two ViewControllers VCItemA and VCItemB.

Now the problem is, I can't change the VCItem from UITabbarControllerB via the SegmentControl from UIViewcontrollerA.

enter image description here


Solution

  • You can try inside ViewcontrollerA ( Swift)

    if let tab = self.children.first as? UITabbarControllerB {
       tab.selectedIndex = // 0 or 1
    }
    

    ( Objective - C )

    if ([self.childViewController.firstObject isKindOfClass:UITabbarControllerB.self])
      UITabbarControllerB* tab = (UITabbarControllerB*) self.childViewController.firstObject;
      tab.selectedIndex = // 0 or 1
    }