【问题标题】:How to call a delegate function from within a custom function?如何从自定义函数中调用委托函数?
【发布时间】:2017-07-28 16:04:56
【问题描述】:

我正在使用 UIPageViewController 并尝试调用委托函数:

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

在自定义函数中:

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

有办法吗?

【问题讨论】:

  • 这是一个委托方法,没有任何事件不会被调用。所以你应该行动一个事件,以便调用这个方法。

标签: ios swift uipageviewcontroller uipagecontrol


【解决方案1】:

在调用函数之前,您需要在视图控制器中设置相关实例的委托,例如

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

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

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

但是,自己调用委托方法是不寻常的。通常它是调用委托的 UIPageViewController 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多