如何从自定义函数中调用委托函数?

问题描述:

我使用的是UIPageViewController,我试图调用委托函数:从自定义函数中如何从自定义函数中调用委托函数?

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

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

有没有办法做到这一点?

+0

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

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

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实例。