我想将UIPageViewController
中的数据传递给其子ViewControllers
之一。 我有一个协议设置像这样:如何将数据从UIPageViewController传递给使用委托的子ViewController
protocol Delegate : class{
func protocolMethod(count:Int)
}
而且在UIPageViewController
:
class PageVC : UIPageViewController{
var delegate : Delegate?
var count : Int = 1
func classMethod(){
self.displayPageForIndex(5) //this controlls the tranistion to the child ViewController
self.delegate?.protocolMethod(count : self.count)
}
}
在符合孩子ViewController
:
class ChildVC : UIViewController , Delegate{
func protocolMethod(count : Int){
print(count)
}
override func viewDidLoad() {
super.viewDidLoad()
let pvc = PageVC()
pvc.delegate = self
}
}
我在做viewDidLoad
什么是最我尝试过的简单解决方案,但仍无法找到答案,以便如何让PageViewController
知道孩子ViewController
是协议方法的接收者。 如果有人可以帮助我完成,而不使用prepareForSegue(因为没有segues),将非常感激。
你的'ChildVC'和'PageVC'被实例化了吗?另外,我会建议为您的代理指定一个名称,以指定它的用途或用途。 – toddg
我有一个函数实例化子VC,然后我将它们添加到数组。 PageVC本身没有实例化(当然,PageViewController的功能完美,所以也许它的实例化方式与子VCS不同)。这里的名字只是占位符,让我知道应该做什么,我在我的真实代码中使用了特定的名字@toddg –
“PageVC本身没有实例化”显然,这里有关于什么类和实例的知识缺口是,以及如何在iOS/Cocoa编程中创建实例。没有什么更基本的东西了:除非知道对象,否则不能进行面向对象的编程。 – matt