0

I have two viewControllers "FirstViewController"&"SecondViewControler" that contain a button that presents a new viewController called "BibliothequesViewController" when a button is clicked:

@IBAction func onLibrariesButtonClicked(_ sender: Any) {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
        self.present(vc, animated: true, completion: nil)
    }

For the time being, the BibliothequesViewController contains a button that's redirects to the FirstViewController with a Segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
           print("SegWay being performed")
           // Every segway has a beginning point and ending point
           // We're trying to access the ending point
           // as type FullScreenMovieController
           // Then we're setting the chosenMovie variable of FullScreenMovieController to 1
           let vc = segue.destination as! FirstViewController
           vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
           print("vc.receivedSelectedLibraryFromBibliothequesList :",  vc.receivedSelectedLibraryFromBibliothequesList )
                }

This is the code that performs the Segue:

  self.performSegue(withIdentifier: "BiblioToLoginSegue", sender: self)

What I need to implement is:
Redirect to the view (FirstViewController or SecondViewControler) that actually presented BibliothequesViewController. The code above automatically redirects to FirstViewController whether it was the one that presented BibliothequesViewController or not.

I don't see which approach should I follow to implement this. Should I use push instead of present in the FirstViewController&SecondViewControler?

AG_HIHI
  • 1,705
  • 5
  • 27
  • 69

2 Answers2

0

The simplest way to teach a class to know something is to add a property:

class BibliothequesViewController {
    ...
    var presentedBy: UIViewController?
    ...
}

to assign the property after the controller instantiation

let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController

vc.presentedBy = self

and to use this property when the time comes.

Roman Ryzhiy
  • 1,540
  • 8
  • 5
  • presentedBy should contain FirstViewController or SecondViewController, that way BibliothequesViewController will know which one presented it. So you answer is right in the beginning but I think the second part is wrong. – AG_HIHI Sep 04 '20 at 13:29
  • I took your advice and tried this :https://stackoverflow.com/questions/63742725/setting-a-viewcontroller-property-by-using-a-shared-instance-remains-nil check it out if you can and tell me what you think – AG_HIHI Sep 04 '20 at 14:06
  • I just implemented a solution with your tip. Thank you. Check it out if you can . – AG_HIHI Sep 04 '20 at 16:25
0

I took @Roman's advice but I changed a couple of things.
CAVEAT: I am not sure this is the right approach but it worked for me.

BibliothequesViewController

class BibliothequesViewController: UIViewController {
    
    static let sharedInstance = BibliothequesViewController()
    var presentedBy: UIViewController?
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if BibliothequesViewController.sharedInstance.presentedBy! is LoginViewController {
            let vc = segue.destination as! LoginViewController
            vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
        } else {
            let vc = segue.destination as! ForgetPasswordViewController
            vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
        }
    }
    
    @IBAction func onLibrarySelected(_ sender: Any) {
        if BibliothequesViewController.sharedInstance.presentedBy! is LoginViewController {
            self.performSegue(withIdentifier: "BiblioToLoginSegue", sender: self)
        } else {
            self.performSegue(withIdentifier: "biblioToPasswordForgottenSigue", sender: self)
        }
    }
    
}

LoginViewController

class LoginViewController: UIViewController {
    @IBAction func onLibrariesButtonClicked(_ sender: Any) {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
        BibliothequesViewController.sharedInstance.presentedBy=self
        self.present(vc, animated: true, completion: nil)
    }
}

ForgetPasswordViewController

  class ForgetPasswordViewController: UIViewController {
        @IBAction func onLibrariesButtonClicked(_ sender: Any) {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
            BibliothequesViewController.sharedInstance.presentedBy=self
            self.present(vc, animated: true, completion: nil)
        }
    }

NOTE: You have to create two Segues from BibliothequesViewController to LoginViewController and ForgetPasswordViewController with the identifiers mentioned in BibliothequesViewController.

AG_HIHI
  • 1,705
  • 5
  • 27
  • 69