我有一个委托函数“getHeight”,它将父视图控制器的高度发送给另一个视图。
我正在接收高度并将其设置为变量“receivedScreenHeight”
这真的很奇怪,我必须做一些非常愚蠢的事情,但变量“receivedScreenHeight”并没有保持不变。
如果我在收到它后立即打印它会返回667.00但如果我打印它作为一个动作的结果它将返回nil。
这是我的代码部分:
protocol GetMyScreenHeightDelegate {
func getHeight(height: CGFloat)
}
class pageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, GetMyScreenHeightDelegate {
var receivedScreenHeight: CGFloat!
func getHeight(height: CGFloat) {
print("the height got sent at... \(height)")
self.receivedScreenHeight = height
print("the height is set at... \(self.receivedScreenHeight)")
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (completed && finished) {
if let currentVC = pageViewController.viewControllers?.last {
let index = vcArr.index(of: currentVC)
print("the height remains at... \(self.receivedScreenHeight)")
//delegate2?.getIndex(index: index!)
if index == 3 {
//self.view.frame.size.height = receivedScreenHeight
self.reloadInputViews()
self.view.setNeedsLayout()
}
}
}
}
这是控制台:
the height got sent at... 667.0
the height is set at... Optional(667.0)
the height remains at... nil
我无处可见,这有变化,但它突然返回零?
-----编辑-----
来自LandingPageViewController的代码段,用于分配委托和调用函数
override func viewDidLoad() {
super.viewDidLoad()
// declare delegate
self.delegate = pageViewController()
// call delegate
let height = self.view.frame.height
delegate?.getHeight(height: height)
左边的LandingPageController,右边的pageViewController