有没有办法通过实例化 - 设置 - 推一个视图控制器使用像这样的辅助方法,但同时避免向下转换?推送UIViewController的帮手方法
func pushController(id: String, setup: (_ vc: UIViewController) ->()) {
if let vc = storyboard?.instantiateViewController(withIdentifier: id) {
setup(vc)
navigationController?.pushViewController(vc, animated: true)
}
}
// usage
pushController(id: "Cars") { vc in
(vc as! CarsVC).brand = "BMW"
}
// ...want to avoid downcasting
vc.brand = "BMW"
周围有铸造没有办法,因为'故事板.instantiateViewController(withIdentifier:)'总是返回类型'UIViewController',你要么投的对象在'pushController(ID:)'方法或施放'storyboard?.i的返回值nstantiateViewController(withIdentifier:)'并且有专门的帮手。 – JAL