Swift 多个故事板 - 如何访问特定的故事板

Swift Multiple Storyboard - How to access specific one

我已经将几个配置文件分成多个故事板以尝试帮助我的冻结时间,因为我听说许多故事板可能是 Xcode 最多 "freeze" 最多 10 个的原因分钟有时会导致我的工作流程崩溃。所以,我需要访问每个特定的故事板,但不知道如何访问。当前代码访问 "Main.Storyboard" 文件,我需要调用其他故事板文件。在这里,我检查是否登录了特定的配置文件并将它们推送到配置文件。我把所有的个人资料都放在一个故事板里,但现在我把它们分开了。只需要知道如何推送到其他故事板。谢谢

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)
                }
            })
        }
    })
}

如果您想访问其他故事板,您需要提供该名称。

self.storyboard 将始终指向您的默认故事板。而是这样做:

let VC = UIStoryboard(name: "VCStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "InstructionScreenController") as! VC
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)

如果您的故事板文件是 "Business.storyboard","your_storyboard_name" 名称应该是 "Business"

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)

使用enumgeneric方法return所需的对象。

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
    }

}

使用此代码实例化您的 viewController

let second = try! AppStoryboard.Second.viewController(viewControllerClass: SecondViewController.self) self.present(second, animated: true, completion: nil)