zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

ios storyboard 获取storyboard中的viewController

ios 获取 Storyboard
2023-09-14 09:04:14 时间

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