0

I had split a couple profiles into multiple storyboards to try to help with my freeze time because I heard to many storyboards can be the cause of why Xcode would just "freeze" for up to 10 mins sometimes causing a crash in my flow of work. So, I need to access each specific storyboard, yet don't know how. This current code accesses the "Main.Storyboard" file yet, I need to call other storyboard files. Here I am checking if a specific profile is signed in and pushing them to the profile. I had all the profiles in one storyboard but now I separated them. Just need to know how to push to other storyboards. Thanks

func checkIfBusinessLoggedIn() {
    Auth.auth().addStateDidChangeListener({ (auth, user) in
        if (user != nil) {
            Database.database().reference().child("Businesses").child((user?.uid)!).observeSingleEvent(of: .value, with: { snapshot in
                if snapshot.exists() {
                    print("Business is Signed In")
                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile")
                    self.present(vc!, animated: true, completion: nil)
                }
            })
        }
    })
}
Lukas Bimba
  • 817
  • 14
  • 35
  • 1
    Possible duplicate of [Instantiate and Present a viewController in Swift](https://stackoverflow.com/questions/24035984/instantiate-and-present-a-viewcontroller-in-swift) – Toma Radu-Petrescu Feb 17 '18 at 09:33

4 Answers4

2
let storyboard = UIStoryboard(name: "your_storyboard_name", bundle: nil)

vc = storyboard.instantiateViewController(withIdentifier: "your_view_controller_name")

//to push that controller on the stack
self.navigationController?.pushViewController(vc, animated: true)

If your storyboard file is "Business.storyboard", "your_storyboard_name" name should be "Business".

Toma Radu-Petrescu
  • 2,152
  • 2
  • 23
  • 57
1

If you want to access other storyboards you need to give that name.

self.storyboard will always point to your default storyboard. Instead do this :

let VC = UIStoryboard(name: "VCStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "InstructionScreenController") as! VC
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
0
Use below code for multipe storybord and create ENUM for storyboard name that is easy to specify storyboard name.  

//MARK:- Enum_StoryBoard
enum enumStoryBoard:String {
    case main = "Main"
    case home = "HomeSB"
}

let storyBoard = UIStoryboard.init(name: enumStoryBoard. home.rawValue, bundle: nil)
            let objLocationSearch = storyBoard.instantiateViewController(withIdentifier: "LocationSearch") as? LocationSearch
            self.navigationController?.pushViewController(objLocationSearch!, animated: true)
Chetu
  • 428
  • 1
  • 7
  • 19
0

Using enum and generic method to return the required object.

enum AppStoryboard : String {

    case First, Second

    var instance : UIStoryboard {
        return UIStoryboard(name: self.rawValue, bundle: Bundle.main)
    }

    func instantiateVC<T : UIViewController>(viewControllerClass : T.Type) throws -> T {

        let storyboardID = (viewControllerClass as UIViewController.Type).storyboardID
        guard let viewObj = instance.instantiateViewController(withIdentifier: storyboardID) as? T else {
         throw ExcpectedError.intantiationErro(msg:"ViewController with identifier \(storyboardID)")
        }
        return viewObj
    }

}

use this code to instantiate your viewController

let second = try! AppStoryboard.Second.viewController(viewControllerClass: SecondViewController.self) self.present(second, animated: true, completion: nil)
Nisarg
  • 1,631
  • 6
  • 19
  • 31
drop remy
  • 1
  • 2