Hide a UIViewController (Screen) in swift

10.3k Views Asked by At

enter image description here

As you can see the screen flow as follows:

After clicking on the 'button2' of 'screen A', app shows 'screen B' from same storyboard which has a tab bar. After clicking on the 'red tab' of 'screen B', app shows the 'green button'(right corner). After clicking on the 'green button' of 'screen B', app shows 'screen C'of Storyboard 2.

I need to go back to 'screen B' of 'Storyboard 1' after clicking the back button of 'screen C', no matter from where exactly 'screen C' has appeared and the 'screen C' of 'Storyboard 2' should disappear from top to bottom.

Is it possible to do so, if yes then how?

2

There are 2 best solutions below

1
On BEST ANSWER

I implement segue and add the animation and it works.

8
On

Yes, you can hide UIViewController, as your UIViewController is nothing but the normal UIView.

So just create an @IBOutlet for your UIView and use .hidden method like this:

 @IBOutlet weak var mainview: UIView!
 override func viewDidLoad() {
    super.viewDidLoad()

   mainview.hidden = true
}

Edit:
Correctly specify what you want to achieve. It does not matter if your display viewController is coming from screen A or B until there should be some condition OR ViewController require some input value (string, int, etc) to process it.

Edit2:
Refer this answer How to check which segue was used

Edit3:
As per your Edit answer (move to ScreenC to ScreenB whether screenC is coming from ScreenA or ScreenB) it is better to used three separate 3 storyboard and call the destination ViewController as needed.

here is code for ViewController class ( moving ScreenA -> ScreenB OR ScreenA -> ScreenC)

@IBAction func buttonAisClick(sender: AnyObject) {
// move to ScreenC directly
    var moveToNextVC:ViewController3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3

    self.presentViewController(moveToNextVC, animated: true, completion: nil)
}


@IBAction func ButtomBisClick(sender: AnyObject) {
// move to ScreenB
    var moveToNextVC:ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2

    self.presentViewController(moveToNextVC, animated: true, completion: nil)
}

code for ViewController2 class( moving from ScreenB -> ScreenC)

@IBAction func ScreenBbuttonIsClick(sender: AnyObject) {
// move to ScreenC
    var moveToNextVC:ViewController3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3

    self.presentViewController(moveToNextVC, animated: true, completion: nil)

}

code for ViewController3 class ( moving from ScreenC -> ScreenB)

@IBAction func ScreenCbuttonIsclick(sender: AnyObject) {
    var moveToNextVC:ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2

    self.presentViewController(moveToNextVC, animated: true, completion: nil)
}