致命错误:在展开可选值 UIPageViewController 时意外发现 nil
Posted
技术标签:
【中文标题】致命错误:在展开可选值 UIPageViewController 时意外发现 nil【英文标题】:fatal error: Unexpectedly found nil while unwrapping an optional value UIPageViewController 【发布时间】:2019-10-29 06:29:36 【问题描述】:所以我想为我的应用程序开发演练屏幕。 这是我迄今为止尝试过的。
我有 3 个视图控制器。
第一个视图控制器有一个 UIView 和两个按钮。 UIView 用于显示滑动内容。 第二个视图控制器有两个文本字段,我想在其中显示一些信息。(DataViewController) 第三个视图控制器是 UIPageViewController。
这里有一些代码可以更好地理解。
第一个 ViewController 的代码。
import UIKit
class ViewController: UIViewController
let dataSource = ["Heading 1","Heading 2","Heading 3","Heading 4"]
@IBOutlet weak var welcomeContentView: UIView!
var currentIndex = 0
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureWelcomeSlides()
func configureWelcomeSlides()
guard let welcomePageViewController = storyboard?.instantiateViewController(withIdentifier: String(describing: WelcomePageViewController.self)) as? WelcomePageViewController else
return
welcomePageViewController.delegate = self
welcomePageViewController.dataSource = self
addChild(welcomePageViewController)
welcomePageViewController.didMove(toParent: self)
welcomePageViewController.view.translatesAutoresizingMaskIntoConstraints = false
welcomeContentView.addSubview(welcomePageViewController.view)
let views: [String: Any] = ["pageView": welcomePageViewController.view]
welcomeContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[pageView]-0-|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: views))
welcomeContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[pageView]-0-|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: views))
guard let startingViewController = detailViewControllerAt(index: currentIndex) else
return
welcomePageViewController.setViewControllers([startingViewController], direction: .forward, animated: true, completion: nil)
func detailViewControllerAt(index: Int) -> WelcomePageContentController?
if index >= dataSource.count || dataSource.count == 0
return nil
guard let welcomePageContentController = storyboard?.instantiateViewController(withIdentifier: String(describing: WelcomePageContentController.self)) as? WelcomePageContentController
else
return nil
welcomePageContentController.index = index
welcomePageContentController.slideHeading!.text = dataSource[index]
return welcomePageContentController
extension ViewController: UIPageViewControllerDelegate, UIPageViewControllerDataSource
func presentationIndex(for pageViewController: UIPageViewController) -> Int
return currentIndex
func presentationCount(for pageViewController: UIPageViewController) -> Int
return dataSource.count
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
let welcomePageContentController = viewController as? WelcomePageContentController
guard var currentIndex2 = welcomePageContentController?.index else
return nil
currentIndex = currentIndex2
if currentIndex2 == 0
return nil
currentIndex2 -= 1
return detailViewControllerAt(index: currentIndex2)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
let welcomePageContentController = viewController as? WelcomePageContentController
guard var currentIndex2 = welcomePageContentController?.index else
return nil
if currentIndex2 == dataSource.count
return nil
currentIndex2 += 1
currentIndex = currentIndex2
return detailViewControllerAt(index: currentIndex2)
DataViewController 的代码(在我的例子中,它被命名为 WelcomePageContentViewController)。
import UIKit
class WelcomePageContentController: UIViewController
@IBOutlet weak var slideHeading: UITextView?
@IBOutlet weak var slideText: UITextView?
var displayText: String!
var index : Int!
override func viewDidLoad()
super.viewDidLoad()
slideHeading!.text = displayText
slideText!.text = displayText
// Do any additional setup after loading the view.
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
*/
UIPageViewController 的代码(在我的例子中,它被命名为 WelcomePageViewController)。
import UIKit
class WelcomePageViewController: UIPageViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
我在 viewdidLoad() 方法之后从 WelcomePageContentController 访问文本字段,但我的应用程序仍然崩溃。
错误消息:致命错误:在展开可选值时意外发现 nil。
崩溃发生在这一行。 welcomePageContentController.slideHeading!.text = dataSource[index] 在 ViewController 类中。
方法:-
func detailViewControllerAt(index: Int) -> WelcomePageContentController?
附上故事板图片。
Xcode 版本(版本 10.1 (10B61)) 斯威夫特(4.2)
【问题讨论】:
它自己声明您在代码中某处返回 nil 的错误。调试并找到它 它的 UITextField 是 nill。我已经仔细检查了故事板中的连接并且连接是有效的。fatal error: Unexpectedly found nil while unwrapping an optional value UIPageViewController
说 pageviewcontroller 是 nil ,对吧?
在项目导航器中,转到 breakpoints
,在左下角 +
添加 Exception breakpoint
并再次运行
【参考方案1】:
当您从情节提要中实例化视图控制器时,实际上尚未加载视图并且尚未设置 IBOutlet
s。您似乎正试图强制访问尚未设置的 UITextView
,这会导致崩溃。
尝试在您的故事板实例化视图控制器后立即添加welcomePageContentController.loadViewIfNeeded()
:
guard let welcomePageContentController = storyboard?.instantiateViewController(withIdentifier: String(describing: WelcomePageContentController.self)) as? WelcomePageContentController
else
return nil
welcomePageContentController.loadViewIfNeeded()
welcomePageContentController.index = index
welcomePageContentController.slideHeading!.text = dataSource[index]
这将加载视图并使用非零值填充您的IBOutlet
s(如果它们确实连接到您的故事板),这将允许您在文本视图上设置文本。
【讨论】:
+1 表示 loadViewIfNeeded。奇迹般有效。谢谢。以上是关于致命错误:在展开可选值 UIPageViewController 时意外发现 nil的主要内容,如果未能解决你的问题,请参考以下文章
SWIFT 致命错误:在展开可选值 (lldb) 时意外发现 nil
JSON 致命错误:在展开可选值 (llbd) 时意外发现 nil