1

I'm an android developer and quiet new to iOS development and have a very basic question I guess.

I have read that it is best practice nowadays to use multiple storyboards for multiple view controllers and navigate through them programmatically like:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"viewController") as! UIViewController
self.present(viewController, animated: true, completion: nil)

Lets say I have two storyboards. In the first one I only have one ViewController and in the second I have two ViewControllers. I launch the app with the first storyboard and with its ViewController. After something like clicking a button I want to present the next storyboard with its first ViewController using the code above. Next ViewController shows up without any problems, but what happened with the first storyboards ViewController? Is it still present in the stack or got it terminated or something like that?

I'm asking this because if I call

dismiss(animated: true, completion: nil)

in the first ViewController of the second storyboard, the ViewController disappears and the old one shows up again. Also if I call this while I want to present the second ViewController in my second storyboard within the first ViewController in my second storyboard, the first ViewController disappears too and presents the ViewController from the first storyboard.

In android, if I want to start the next Screen/Activity I do it with:

startActivity(new Intent(CurrentActivity.this, NextActivity.class));
finish(); //to drop the current activity

And while I'm inside the NextActivity and clicking the back button, the whole application will minimize.

I just want to make sure to not build up a huge stack of ViewControllers in my application.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kai Kuhlmann
  • 172
  • 13

2 Answers2

0

So I assume you want to go VC1 -> VC2 -> VC3. When you present VC3 there is no need to dismiss VC2. This creates a navigation stack. When you dismiss VC3, VC2 will still be there and same with dismissing VC2, VC1 will be there. If you want instead VC1 to present VC3, when you dismiss VC2 from VC1 you need to present VC3 from the completion block of the dismiss call.

Slayter
  • 1,172
  • 1
  • 13
  • 26
  • Thanks for the answer. I know how to present ViewControllers. But do I have to dismiss every VC if I start another one? Like: VC1 -> VC2 (dismiss VC1) -> VC3 (dismiss VC2) -> instantiate VC2 (dismiss VC3) and so on – Kai Kuhlmann Jun 28 '17 at 19:33
  • And what if I have lets say 100 VCs. Will they all be on the stack if i presented them once? – Kai Kuhlmann Jun 28 '17 at 19:35
  • 1
    I think you need to think about the navigation of your app. If your just jumping from VC to VC it doesn't stay in line with the usual iOS flow. Usually you present a VC the user does things there and dismisses it. Or you have a `UINavigationController` which pushes VC's onto the stack and the user can navigate forward or back. Or a navigation drawer to switch the VC. But your current flow will eventually be a pain. – Slayter Jun 28 '17 at 19:37
  • Okay that sounds good. What if I have two main parts of the app.. like login/signup/terms/etc in one storyboard using a single UINavigationController and the other part is the main part of the application and has a UINavigationController too and I switch from the first storyboard to the second one, do I have to dismiss the first controller? – Kai Kuhlmann Jun 28 '17 at 19:42
  • No. You can look into switching the current view controller for the `UIWindow` of the app. Something like this: https://stackoverflow.com/questions/41144523/swap-rootviewcontroller-with-animation – Slayter Jun 28 '17 at 19:44
  • Do you maybe have a link for me for best practice workflow with ViewControllers? – Kai Kuhlmann Jun 28 '17 at 19:58
  • https://developer.apple.com/ios/human-interface-guidelines/interaction/navigation/ – Slayter Jun 28 '17 at 19:59
0

Sorry to say that but it is a bad practice to use multiple storyboards if Xcode is powering with the autolayout

You just simply use autolayout.

Example: If u have a UIButton in your view controller and you want to decrease and increase as per view of ViewController. Then add constraint i have suggested below

  1. Give some aspect ratio to the Button.
  2. From view Hierarchy in the left of interface builder access the button and then ctrl+drag from button to the parent view, by holding command select "equal height" , "center Vertically in container", "center horizontally in container" and then click on "Add Constraint"
  3. Then select your button open size inspector and and double click on "height constraint" and give multiplier as per your need(note: it is directly proportional to parent view's height).
  4. If you want to shift button in the left of the center of parent view then double click on "Align center X" and give multiplier below the 1(as per your need).
  5. if you want to shift your button above the center then set multiplier below 1 for "Align center Y" constraint.

That's it. Just run and check.

If you want to switch between two ViewControllers

  1. Write below code in the action of first ViewController's button.

        let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "YourSecondVCIdentifier")
    
        self.present(secondVC!, animated: true, completion: nil)
    
  2. And write below code in the action of SecondViewController's Button.

        self.dismiss(animated: true, completion: nil)
    
Amit Kumar
  • 583
  • 5
  • 16
  • That's not answering my question. You're telling me that it is not best practice to use multiple storyboards, but why exactly? Everything else in your answer was something I already knew and didn't ask for that. – Kai Kuhlmann Jun 30 '17 at 07:13