ios - 使用 panGestureRecognizer 进行滑出 View 时获取 "unexpectedly found nil while unwrapping an Optional value"

标签 ios swift viewcontroller uipangesturerecognizer

我已经滑出 View Controller 并且它工作得很好,直到 View Controller 调用 performSegue 到另一个 View Controller 。我在引用 panGestureRecognizer 时遇到以下错误,它在父 View Controller 中用于滑出。

fatal error: unexpectedly found nil while unwrapping an Optional value

这是可能导致问题的代码,此代码位于目标 View Controller 中:

sidebarButton.target = self.revealViewController()
sidebarButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

当我评论上面的第 3 行时,到目标 View Controller 的 segue 有效,但目标 View Controller 的 side out 函数不起作用。希望这些额外的细节有所帮助。

最佳答案

假设该字符串是一个有效的键。

您正在处理可选值,因此在使用之前有条件地解包每个返回对象。

强制展开是危险的,只有在您绝对确定可选包含值时才应使用。

关于ios - 使用 panGestureRecognizer 进行滑出 View 时获取 "unexpectedly found nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394464/

相关文章:

ios - 应用商店提交错误 Invalid Bundle Structure

ios - 将 Range<Int> 转换为 Range<String.Index>

iOS 开发 : Using a custom image for the user annotation in MapBox

ios - 连接丢失时显示 UIAlertView

ios - 关闭所有非当前 View

swift4 - 如何在 Swift 4 中设置新的 Root viewController?

ios - 如何在 ios 中检查互联网是否正常工作

iphone - 有没有办法将 iPhone 和 iPad 应用程序(在应用程序商店中作为单独的应用程序提供)合并为 1 个通用应用程序

swift - 如何在 Swift 中访问 ScrollView 中的各个 View

ios - 为什么当我结合 UIImagview 时我的模糊 View 变得透明?