Custom init for UIViewController in Swift with interface setup in storyboard

后端 未结 12 2110
长情又很酷
长情又很酷 2020-11-27 05:08

I\'m having issue for writing custom init for subclass of UIViewController, basically I want to pass the dependency through the init method for viewController rather than se

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 05:22

    // View controller is in Main.storyboard and it has identifier set

    Class B

    class func customInit(carType:String) -> BViewController 
    
    {
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let objClassB = storyboard.instantiateViewController(withIdentifier: "BViewController") as? BViewController
    
        print(carType)
        return objClassB!
    }
    

    Class A

    let objB = customInit(carType:"Any String")
    
     navigationController?.pushViewController(objB,animated: true)
    

提交回复
热议问题