Vasu1604
Vasu1604

Reputation: 25

Presenting ViewController Dynamically

i am presenting a ViewController like this

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

Upvotes: -1

Views: 273

Answers (1)

Imran Rasheed
Imran Rasheed

Reputation: 956

Create New File 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

    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

Upvotes: 0

Related Questions