如何在函数参数中传递 viewcontroller 名称?

How to pass viewcontroller name in function parameters?

func showalertOkayButton(message: String?, identifier: string?, viewControllerName: UIViewController){

    let alertController = UIAlertController(title: kMessage, message: message, preferredStyle: .alert)
       let defaultaction = UIAlertAction(title: kOkay, style: UIAlertAction.Style.default, handler:{ defaultaction in
        let VC = UIStoryboard(name: kMainStoryBoard, bundle: nil).instantiateViewController(withIdentifier: identifier) as! viewControllerName
        self.navigationController?.pushViewController(VC, animated: true)
    })
    alertController.addAction(defaultaction)
    present(alertController, animated: true, completion: nil)
}

所以我的问题是我无法发送 viewControllerName 它给我错误 "used of undeclared" 那么我如何在函数中发送视图控制器名称。 好吧,这是为了学习目的,对不起,如果我问错了。 所以请指导我或帮助我 感谢 advacne.

您误解了 as 的工作原理。在 x as! y 中,y 不是 name,而是 type。当您将 viewControllerName: UIViewController 作为函数参数时,它不会传递一个 name 而是传递一个 instance 的 UIViewController 类型。因此,如果你想指定 as 转换到什么,你需要传递一个 type 作为你的函数参数。使用泛型,看起来像这样:

func showalertOkayButton<T: UIViewController>(message: String?, identifier: String, viewControllerType: T.Type) {
    let alertController = UIAlertController(title: kMessage, message: message, preferredStyle: .alert)

    let defaultaction = UIAlertAction(title: kOkay, style: UIAlertAction.Style.default, handler:{ defaultaction in
        let VC = UIStoryboard(name: kMainStoryBoard, bundle: nil).instantiateViewController(withIdentifier: identifier) as! T
        self.navigationController?.pushViewController(VC, animated: true)
    })

    alertController.addAction(defaultaction)
    present(alertController, animated: true, completion: nil)
}

调用它看起来像这样

showalertOkayButton("Message", "VCIdentifier", MyViewController.self)

但是,none 在您的特定情况下这是必需的。 Swift 是一种动态语言,因此当您从情节提要中实例化视图控制器时,它可以在运行时加载其动态类型信息。但是 compiler 不知道它是什么类型,因此它给你一个简单的 UIViewController 实例作为结果。在您需要在该视图控制器上调用 UIViewController 超类中不存在的特定方法之前,没有必要使用 as 对其进行转换。

即使您不转换它,由于多态性,它也会继续正确运行。所以你可以把你的代码缩短到这个

func showalertOkayButton(message: String?, identifier: String) {
    let alertController = UIAlertController(title: kMessage, message: message, preferredStyle: .alert)

    let defaultaction = UIAlertAction(title: kOkay, style: UIAlertAction.Style.default, handler:{ defaultaction in
        let VC = UIStoryboard(name: kMainStoryBoard, bundle: nil).instantiateViewController(withIdentifier: identifier)
        self.navigationController?.pushViewController(VC, animated: true)
    })

    alertController.addAction(defaultaction)
    present(alertController, animated: true, completion: nil)
}

Swift 是静态的,不是动态的。所有类型都必须事先知道。您不能传递未知类型并在运行时说“转换为这种类型”。但你不需要!只需删除有关视图控制器的内容 "name"(实际上是它的 class)。您可以在不知道其具体 class 的情况下推送视图控制器。这是一个视图控制器!编译器知道这一点,这就是它需要知道的全部。只需删除有关视图控制器类型的内容,一切都会好起来的。

func showalertOkayButton(message: String?, identifier: String)
    let alertController = UIAlertController(title: kMessage, message: message, preferredStyle: .alert)
    let defaultaction = UIAlertAction(title: kOkay, style: UIAlertAction.Style.default, handler:{ defaultaction in
        let VC = UIStoryboard(name: kMainStoryBoard, bundle: nil).instantiateViewController(withIdentifier: identifier)
        self.navigationController?.pushViewController(VC, animated: true)
    })
    alertController.addAction(defaultaction)
    present(alertController, animated: true, completion: nil)
}