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

问题描述 投票:0回答:1
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",所以我怎么能在函数中发送视图控制器的名称.好吧,这是为了学习的目的,如果我问错了,对不起,所以请指导我或帮助我Thank in advacne.

ios swift function alert
1个回答
2
投票

你误解了 as 作品。在 x as! y,y不是一个 名称 它是 类型. 而当你有 viewControllerName: UIViewController 作为函数参数,也就是说没有传递一个 名称 正在通过 实例 的UIViewController 类型. 所以,如果你想明确什么是 as 投向,你需要通过一个 类型 作为你的函数参数。使用泛型,它看起来像这样。

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)

然而,在你的具体案例中,这些都不是必须的。Swift是一种动态语言,所以当你从storyboard实例化一个视图控制器时,它可以在运行时加载其动态类型信息。然而 编译者 不知道它是什么类型,所以它给你一个简单的 UIViewController 实例作为结果。不需要使用 as 直到你需要在该视图控制器上调用特定的方法,而这些方法在 UIViewController 超类。

即使你不投弃它,由于多态性,它也会继续正确地表现。所以你可以将你的代码缩短为

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)
}

1
投票

Swift是静态的,不是动态的。所有类型都必须事先知道。你不能传递一个未知的类型,然后在运行时说 "投到这个类型"。但是你不需要! 只要把视图控制器的 "名字"(实际上是它的类)剥离出来就可以了。你可以推送一个视图控制器而不知道它的具体类。它是一个视图控制器! 编译器知道这一点,这就是它需要知道的。只要删除你关于视图控制器类型的东西,一切都会好起来的。

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.