我的应用程序在ios8上运行良好。但是在将所有内容更新到ios9后,我遇到了nill值的问题。我无法弄清楚什么价值是零。这是代码:
func resetToChosenPage(index: Int!) {
/* Getting the page View controller */
pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
pageContentViewController = self.viewControllerAtIndex(index)
if pageContentViewController != nil {
self.pageViewController.setViewControllers([pageContentViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
}
/* We are substracting 30 because we have a start again button whose height is 30*/
self.pageViewController.view.frame = CGRectMake(0, self.navigationController!.navigationBar.frame.height , self.view.frame.width, self.view.frame.height - 30)
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
func viewControllerAtIndex(index : Int) -> UIViewController? {
if((dataArray.count == 0) || (index >= dataArray.count)) {
return nil
}
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SingleArticleViewController") as! SingleArticleViewController
self.delegate = pageContentViewController
pageContentViewController.delegate = self
// pageContentViewController.delegateWebView = self
pageContentViewController.singleArticleIndex = index
pageContentViewController.categoryID = categoryID
pageContentViewController.dateOfPub = dataArray[index].pubDate
pageContentViewController.category = dataArray[index].category
pageContentViewController.newsTitle = dataArray[index].pageTitle
pageContentViewController.videoLink = dataArray[index].videoLink
return pageContentViewController
}
一行self.pageViewController.SetViewControllers([pageContentViewController] ...我有错误"致命错误:在解包一个可选值"时意外地发现了nil。事实上这个代码在ios8上运行良好。我检查并找不到nil值。此外,我检查了故事板的所有标识符,但它们是正确的。伙计们,如果此代码适用于ios8,会出现什么问题?
答案 0 :(得分:0)
我建议尝试更改包含if
调用的setViewControllers:
子句,以便它显式检查pageContentViewController和pageViewController:
if let pageVC = self.pageViewController, let contentVC = self.pageContentViewController {
pageVC.setViewControllers([contentVC], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
}
作为一般情况,我会尽量避免在resetToChosenPage:
方法中使用Implicitly Unwrapped Optionals(就像Int !.因为它们会导致这些意外发现的错误。
此外,由于您的resetToChosenPage:
方法不检查pageViewController是否已经实例化,因此多次调用它将导致许多PageViewController实例被实例化并堆叠在彼此之上。这可能不是你想要的。