gpt4 book ai didi

ios - 如何从自定义函数中调用委托(delegate)函数?

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

我正在使用 UIPageViewController 并且我正在尝试调用委托(delegate)函数:

    public func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
}

从自定义函数中:

func timer () {
//Call UIPageViewController, willTransitionTo here
}

有办法吗?

最佳答案

在调用函数之前,您需要在 View Controller 中设置相关实例的委托(delegate),例如

let pvc = UIPageViewController()
pvc.delegate = self // assumes that the class adopts UIPageViewController delegate protocol and adheres to it

然后您可以访问该委托(delegate),然后调用该委托(delegate)上的任何方法,如下所示:

func timer () {
let pvcDelegate = pvc?.delegate
// use pvcDelegate followed by function you wish to call
}

但是,您自己调用委托(delegate)方法是不常见的。通常是 UIPageViewController 实例调用委托(delegate)。

关于ios - 如何从自定义函数中调用委托(delegate)函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45377940/

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