gpt4 book ai didi

ios - 如何使用 swift 将 UIVIewController 名称作为参数传递给特定函数?

转载 作者:行者123 更新时间:2023-11-29 05:16:42 24 4
gpt4 key购买 nike

在我的场景中,我需要将 UIVIewController 名称和更多字符串值传递给特定的函数。我尝试了下面的代码但没有得到结果。

将参数传递给特定函数

self.accountoptionscall(vcName: UIViewController(), vcIdentifier: "profileviewcontroller", popUpVC: ProfileViewController.self)

func accountoptionscall<T: UIViewController>(vcName: UIViewController,vcIdentifier: String, popUpVC: T.self) {
let viewcontrollers = self.storyboard!.instantiateViewController(withIdentifier: vcIdentifier) as! vcName
let navController = UINavigationController(rootViewController: viewcontrollers)
self.present(navController, animated:true, completion: nil)
}

最佳答案

我在我的应用程序中使用它,我认为它可以帮助你。

extension UIViewController {

/// Load UIViewController type from UIStoryboard
class func loadFromStoryboard<T: UIViewController>() -> T {
let name = String(describing: T.self)
let storybord = UIStoryboard(name: name, bundle: nil)
if let viewController = storybord.instantiateInitialViewController() as? T {
return viewController
} else {
fatalError("Error: No initial view controller in \(name) storyboard!")
}
}
}

这里是如何使用它:

  func loadVC<T: UIViewController>(controller: T) {
let vc: T = T.loadFromStoryboard()
let navigationVC = UINavigationController(rootViewController: vc)
self.present(navController, animated:true, completion: nil)
// or if use in appDelegate you can do it: window?.rootViewController = navigationVC
}

关于ios - 如何使用 swift 将 UIVIewController 名称作为参数传递给特定函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59115827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com