iosswiftxlpagertabstrip

Swift: XLPagerTabStrip: ButtonBarPagerTabStripViewController: ChildViewController IBOutlets are not set


Xcode -> 9.4.1, iOS -> 11.4, XLPagerTabStrip -> 8.0.1

I wanted to create top scrolling tabs ( like in android ) in ios using swift.

I am using ButtonBarPagerTabStripViewController from library XLPagerTabStrip. Below is the code :-

class ParentViewController: ButtonBarPagerTabStripViewController {

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {

        return [ChildOneViewController(), ChildTwoViewController(), ChildThreeViewController()]

    }

}

Now everything works fine, three scrolling tabs are created for three childviewcontrollers, changing on swipe left and right. But when i created a label in storyboard for first childviewcontroller and created a IBOutlet for this label in ChildOneViewController class. This label is not set and printing nil in viewdidload, viewdidappear. Code is like this :-

class ChildOneViewController: UIViewController, IndicatorInfoProvider {

    @IBOutlet weak var label: UILabel!   // This label i have set from storyboard.

    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {

        return IndicatorInfo(title: "CHILD ONE")

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print(self.label) // This is printing nil to console.

   }

   override func viewDidAppear(_ animated: Bool) {
       super.viewDidAppear(animated)

       print(self.label) // This is also printing nil to console.

   }

}

Solution

  • If the vcs are created in IB , then don't use

    ChildOneViewController()
    

    but use

    let ch1 = self.storyboard?.instantiateViewController(withIdentifier: "childOneId") as! ChildOneViewController
    

    Give each one an identifier and load it as above