-1

I need to switch Page View Controller using a button. The initialViewController is the CustomPageViewController.

I tried this in the View Controller :

class MainViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}    
@IBAction func goProfile(_ sender: Any) {
    CustomPageViewController().goToProfile()
}

And this in the custom class PageViewController :

class CustomPageViewController: UIPageViewController {

fileprivate lazy var pages: [UIViewController] = {
    return [
        self.getViewController(withIdentifier: "MainViewController"),
        self.getViewController(withIdentifier: "ProfilViewController")
    ]
}()

fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
{
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
}

override func viewDidLoad()
{
    super.viewDidLoad()
    self.dataSource = self
    self.delegate   = self

    if let firstVC = pages.first
    {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }
}

func goToProfile(){
    setViewControllers([pages.last!], direction: .forward, animated: true, completion: nil)
}

But nothing happens, any ideas ? Thanks

EDIT : Final MainViewController's code working

@IBAction func goProfile(_ sender: Any) {
    let vc = UIApplication.shared.keyWindow?.rootViewController as! CustomPageViewController
    vc.goToProfile()
}
R1'
  • 490
  • 1
  • 5
  • 17

1 Answers1

1

The problem is that this line is wrong:

CustomPageViewController().goToProfile()

The expression CustomPageViewController() creates a new, separate custom page view controller, which never appears in the interface and is thrown away in the next line.

What you need is a reference to an actual custom page view controller that is in your interface.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ok thanks, and how can I make this reference ? – R1' Feb 14 '18 at 17:51
  • I have no idea. You have not revealed the view controller hierarchy of your view controllers. I don't know the relationship between your MainViewController and your CustomPageViewController. Only you know that. I don't even know if, at the time the user is in your MainViewController, there _is_ a CustomPageViewController. But the reason I have given you — and so have two other people — is the reason your code does nothing. Think about it. Learn what classes and instances are. Learn the difference between reference and instantiation. – matt Feb 14 '18 at 17:55
  • Might want to read http://www.apeth.com/swiftBook/ch04.html#SECinstanceRefs where I discuss the mistake you are making, in detail – matt Feb 14 '18 at 17:56
  • 1
    and if u marked it as a duplicate y answering – Shehata Gamal Feb 14 '18 at 17:57
  • 2
    @Sh_Khan Actually what I don't understand is why you deleted your answer, which was quite right. – matt Feb 14 '18 at 17:59
  • Thanks for reply. The initialViewController is the CustomPageViewController, it shows the MainViewController with the code I edited in the post – R1' Feb 14 '18 at 17:59
  • @Sh_Khan Can you repost your answer which was helpful, I will set a +1 if it works, and I don't know who voted -1 – R1' Feb 14 '18 at 18:00
  • @matt According to your tutorial, I should make this : UIApplication.shared.keyWindow?.rootViewController.goToProfile() ? – R1' Feb 14 '18 at 18:06
  • No. According to my tutorial you should _think_. That's just an example for one particular situation. Figuring out how to get the reference is something only you can do, because only you know the relationship between the two view controllers. – matt Feb 14 '18 at 18:08