斯威夫特3/Xcode 8.1
以下代码...
class PageContentVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
var array = [String]()
var currentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
array = ["1","2","3","4"]
self.dataSource = self
self.delegate = self
self.setViewControllers([helperFunction(index: currentIndex)], direction: .forward, animated: true, completion: nil)
}
func helperFunction(index: Int) -> UIViewController{
let newVC = storyboard?.instantiateViewController(withIdentifier: "PageContent") as! PageContent
print("currentIndex is: \(currentIndex)")
print("value at currentIndex is: \(array[currentIndex])")
newVC.PageContentLabel.text = array[currentIndex]
return newVC
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
if (currentIndex == 0) || (currentIndex == NSNotFound) {
return nil
}
currentIndex -= 1
return helperFunction(index: currentIndex)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
if (currentIndex == array.count - 1) || (currentIndex == NSNotFound) {
return nil
}
currentIndex += 1
return helperFunction(index: currentIndex)
}
}
...在以下行中使用“Unexpectedly found nil while unwrapping an optional value”错误使应用程序崩溃:
newVC.PageContentLabel.text = array[currentIndex]
“PageContentLabel”肯定是通过界面生成器链接的,当我注释掉有问题的行时,应用程序运行良好。
我在这里想念什么?