简体   繁体   English

动态呈现 ViewController

[英]Presenting ViewController Dynamically

i am presenting a ViewController like this我正在展示一个这样的 ViewController

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

is there any way to pass storyboard name,identifier, and View Controller type dynamically有没有办法动态传递故事板名称、标识符和视图控制器类型

Create New File StoryBoard.swift创建新文件 StoryBoard.swift

let mainBundle = Bundle.main
enum Storyboard: String {
case main = "Main"
case TeamLeader = "TeamLeader"
case Installer = "Installer"
}

extension Storyboard {
var instance: UIViewController {
    return UIStoryboard(name: Storyboard.main.rawValue, bundle: mainBundle).instantiateViewController(withIdentifier: self.rawValue)
}
}

Create New ControllerIdentifier.swift File创建新的 ControllerIdentifier.swift 文件

    enum ControllerIdentifier: String {
    case CustomerDetailsVC
    case TeamVC
    case MyProfileVC
    case CancelOrderVcViewController
    case ApprovingCustomerVC
    case RequestsDetailVC
    case NewrequestTL
    case ApprovedTLVC
    case HistoryTLVC
    case SettingsTeamleaderVC
    case HistoryDetailVC
    case ApprovingCustomerFirstVC
    case ApprovingCustomerSecondVC
    case ApprovingCustomerForthVC
    case ApprovingCustomerFifthVC
    case tabbar2
    case ApprovingCustomerThridVC
    case SignatureVC
    case NewRequestsVC
    case tabbar
    case LoginVC
    case CustomerDetailsTL
    case InstallationApprovingFirstVC
    case InstallationApprovingSecondVC
    case InstallerDashBoardVC
    case tabbar3
    case HistoryInstallerVC
    case OrderInProgressVC
    case SettingsInstallerVc
    case RequestDetailInstallerVC
    case HistoryDetailsInstallerVC
    case LocationInstallerVC
    case CancelOrderInstallerVC
    case MyProfileInstallerVC
    
}

extension UIViewController{
    func pushToController(from name : Storyboard, identifier: ControllerIdentifier) {
        DispatchQueue.main.async { [self] in
            let storyboard = UIStoryboard(name: name.rawValue, bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: identifier.rawValue)
            navigationController?.pushViewController(vc,animated: true)
        }
    
    }
    func pushToRoot(from name : Storyboard, identifier: ControllerIdentifier) {
        DispatchQueue.main.async { [self] in
        let storyboard = UIStoryboard(name: name.rawValue, bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: identifier.rawValue)
        let navigationController = UINavigationController(rootViewController: vc)
        navigationController.modalPresentationStyle = .fullScreen
        self.present(navigationController, animated: true, completion: nil)
        }
        
    }
    
    func imageScreenPush(from name : Storyboard, identifier: ControllerIdentifier) {
        DispatchQueue.main.async { [self] in
        let storyboard = UIStoryboard(name: name.rawValue, bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: identifier.rawValue)
        let navigationController = UINavigationController(rootViewController: vc)
            navigationController.modalPresentationStyle = .overCurrentContext
        self.present(navigationController, animated: true, completion: nil)
        }
        
    }
}

How To Use如何使用

self.pushToController(from: .Installer, identifier: .tabbar3)

Make sure StoryBoard Name and UIViewController Identifier should be same in Enum cases确保 StoryBoard Name 和 UIViewController Identifier 在 Enum 情况下应该相同

Storyboard Extension:故事板扩展:

extension UIStoryboard {
    enum Name: String {
        case main = "Main"
        case home = "Home"
    }

    static func instantiateViewController<T>(storyboard name: Name = .main, ofType type: T.Type) -> T? {
        return UIStoryboard(name: name.rawValue, bundle: nil).instantiateViewController(withIdentifier: String(describing: type.self)) as? T
    }
}

Usage: (Make sure Storyboard Id and class-name are the same.)用法:(确保 Storyboard Id 和 class-name 相同。)

    if let vc = UIStoryboard.instantiateViewController(storyboard: .home, ofType: HomeVC.self) {
        self.present(vc, animated: true, completion: nil)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM