Swift 如何重用传递ViewController名称的代码
我正在使用swift语言编程,我希望重用此代码,将标识符作为字符串传递,并将页面作为视图控制器名称传递,但我遇到错误使用未声明的类型“page”,我如何实现这一点?谢谢Swift 如何重用传递ViewController名称的代码,swift,reusability,code-reuse,Swift,Reusability,Code Reuse,我正在使用swift语言编程,我希望重用此代码,将标识符作为字符串传递,并将页面作为视图控制器名称传递,但我遇到错误使用未声明的类型“page”,我如何实现这一点?谢谢 func toReuseSession(identifier: String, **page**: UIViewController){ let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
func toReuseSession(identifier: String, **page**: UIViewController){
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainVC = mainStoryBoard.instantiateViewController(withIdentifier: identifier) as? **page** else {
return
}
mainVC.modalPresentationStyle = .fullScreen
present(mainVC, animated: true, completion: nil)
}
对于泛型,这是一种可能的解决方案,但必须在第二个参数中传递静态类型
extension UIViewController {
func toReuseSession<T>(identifier: String, page: T.Type) where T : UIViewController {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: .main)
guard let mainVC = mainStoryBoard.instantiateViewController(withIdentifier: identifier) as? T else {
return
}
mainVC.modalPresentationStyle = .fullScreen
present(mainVC, animated: true, completion: nil)
}
}
扩展UIViewController{
func toReuseSession(标识符:字符串,页面:T.Type),其中T:UIViewController{
让mainStoryBoard=UIStoryboard(名称:“Main”,捆绑包:.Main)
guard let mainVC=mainStoryBoard.InstanceeviewController(带标识符:标识符)作为?T else{
返回
}
mainVC.modalPresentationStyle=.fullScreen
当前(mainVC,动画:真,完成:无)
}
}
对于泛型,这是一种可能的解决方案,但您必须在第二个参数中传递静态类型
extension UIViewController {
func toReuseSession<T>(identifier: String, page: T.Type) where T : UIViewController {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: .main)
guard let mainVC = mainStoryBoard.instantiateViewController(withIdentifier: identifier) as? T else {
return
}
mainVC.modalPresentationStyle = .fullScreen
present(mainVC, animated: true, completion: nil)
}
}
扩展UIViewController{
func toReuseSession(标识符:字符串,页面:T.Type),其中T:UIViewController{
让mainStoryBoard=UIStoryboard(名称:“Main”,捆绑包:.Main)
guard let mainVC=mainStoryBoard.InstanceeviewController(带标识符:标识符)作为?T else{
返回
}
mainVC.modalPresentationStyle=.fullScreen
当前(mainVC,动画:真,完成:无)
}
}
根据您的功能,您应该被声明为UIViewController的通用类型,这样您就可以实现输出。我已经用正确的语法修改了您的函数,您可以使用它:-
func toReuseSession<T:UIViewController>(identifier: String, page: T){
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainVC = mainStoryBoard.instantiateViewController(withIdentifier: identifier) as? T else {
return
}
mainVC.modalPresentationStyle = .fullScreen
present(mainVC, animated: true, completion: nil)
}
self.toReuseSession(identifier: "NewPasswordViewController", page: NewPasswordViewController())
//“NewPasswordViewController”在我检查它是否工作时出现。您可以更改ViewController想要显示的任何内容。根据您的功能,您应该被声明为UIViewController的通用类型,这样您就可以实现输出。我已经用正确的语法修改了您的函数,您可以使用它:-
func toReuseSession<T:UIViewController>(identifier: String, page: T){
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainVC = mainStoryBoard.instantiateViewController(withIdentifier: identifier) as? T else {
return
}
mainVC.modalPresentationStyle = .fullScreen
present(mainVC, animated: true, completion: nil)
}
self.toReuseSession(identifier: "NewPasswordViewController", page: NewPasswordViewController())
//“NewPasswordViewController”在我检查它是否工作时出现。您可以更改ViewController想要显示的任何内容。对不起,我还有一个问题,如果我想通过modalPresentationStyle,我该怎么做?对不起,我还有一个问题,如果我想通过modalPresentationStyle,我该怎么做?