iosswiftxcodexlpagertabstrip

navigating to another child on button click


am using https://github.com/xmartlabs/XLPagerTabStrip#how-to-change-the-visible-child-view-controller-programmatically in my application ...

on button click from one child i want to move to another child .. i tried:

@IBAction func morenewsbtn(_ sender: Any) {
    print("clicked")
    let mainpage = HomeViewController()
    mainpage.moveToViewController(at: 1, animated: true)
}

but this will not move to another child .. tried this in viewdidappear and its working fine .. but how to use it in button click?


Solution

  • Because this

    let mainpage = HomeViewController() // problem is here
    mainpage.moveToViewController(at: 1, animated: true)
    

    is a new VC , you need to make it from the presented one , you can either set a reference to home , or simply use this

    let home = self.parent as! HomeViewController
    parent.moveToViewController(at: 1)