ios storyboard 获取storyboard中的viewController

1.初版

import UIKit

class CBhelpManager: NSObject {

    static let shared = CBhelpManager()
    
    func getViewController(storyboard: String, viewController: String) -> UIViewController {
        let storyboard =  UIStoryboard.init(name: storyboard, bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: viewController)
        return vc
    }
}

2.优化版

import UIKit

class XYHelper: NSObject {
    
    static func getViewController(storyboardStr: String?, viewController: String) -> UIViewController {
        var storyboard = UIStoryboard()
        if let storyboardStr = storyboardStr {
            storyboard =  UIStoryboard.init(name: storyboardStr, bundle: nil)
        }else {
            storyboard =  UIStoryboard.init(name: "Main", bundle: nil)
        }
        let vc = storyboard.instantiateViewController(withIdentifier: viewController)
        return vc
    }
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页