如何解决每当我在 tableView 行中选择一行时,Swift 都无法到达目标类
简单的情况。每当我在 View1 中选择一行时,我都会崩溃。
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "NextScreen",bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "NextScreenViewController") as! NextScreenViewController
self.navigationController?.pushViewController(controller,animated: false)
}
我确实有一个 NextScreen.xib,它是这个类 NextScreenViewController 的所有者 帮助将不胜感激!
解决方法
您的代码和您的描述不匹配。您的代码要求您有一个名为“NextScreen”的 Storyboard 文件,其中包含一个标识符为“NextScreenViewController”的视图控制器,它是类型为 NextScreenViewController
的 UIViewController 的子类。
您的描述说您有一个“是 NextScreenViewController 类的所有者”的 NextScreen.xib。您的代码并未尝试从 XIB 加载视图控制器。它试图从故事板加载它。是哪个?
XIB 文件和故事板是在项目中包含视图的两种不同方式。如果将视图控制器的视图保存在 XIB 中,则需要从 XIB 加载它们。如果将它们保存在故事板中,则需要从故事板加载它们。 如果您将视图控制器的视图保存在 XIB 文件中,然后尝试从故事板加载它们,您的应用将崩溃。
您添加到问题中的崩溃日志清楚地表明您尝试加载的故事板不存在,这就是您崩溃的原因。它说“找不到名为‘PlacesPlusScreen’的故事板。”证明是肯定的。
您的项目中是否有名为“NextScreen”的 Storyboard 文件或 XIB 文件?什么线路崩溃?确切的错误消息是什么?
尝试像这样改变你的函数:
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "NextScreen",bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: "NextScreenViewController") as? NextScreenViewController else {
print("Can't load storyboard with ID 'NextScreenViewController'")
return
}
self.navigationController?.pushViewController(controller,animated: false)
}
注意你的线
let storyboard = UIStoryboard(name: "NextScreen",bundle: nil)
如果它无法加载您的故事板文件,将使您的程序崩溃并抛出一个异常,描述为“在捆绑包中找不到名为‘NextScreen’的故事板...”。
因此,您再次需要查看控制台中的确切崩溃消息,如果您需要我们帮助调试问题,请在此处报告。
,这是它崩溃的地方
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "PlacesPlusScreen",bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: "PlacesPlusScreenController") as? PlacesPlusScreenController else {
print("Can't load storyboard with ID 'PlacesPlusScreenController'")
return
}
self.navigationController?.pushViewController(controller,animated: false)
}
这是课堂
class PlacesPlusScreenController: UIViewController
这是 xib 文件和 PlacesPlusScreen 所在的层次结构。
07-22 20:49:44.080494-0700 Example[14820:6956631] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: 'Could not find a storyboard named 'PlacesPlusScreen' in bundle NSBundle </private/var/containers/Bundle/Application/87BBA05A-9FF9-4851-A4FB-05DF7938E232/Example.app> (loaded)'
*** Ffirst throw call stack:
(0x184c9e754 0x1997657a8 0x187810ffc 0x102fbca0c 0x102fbc730 0x102fbcaec 0x18789f1d0 0x18789ed78 0x18789f538 0x187b69c98 0x18769c6c4 0x18768b03c 0x1876bef10 0x184c175e0 0x184c11704 0x184c11cb0 0x184c11360 0x19c24f734 0x18768c584 0x187691df4 0x102fc9478 0x1848cdcf8)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: 'Could not find a storyboard named 'PlacesPlusScreen' in bundle NSBundle </private/var/containers/Bundle/Application/87BBA05A-9FF9-4851-A4FB-05DF7938E232/JacquardSDK_Example.app> (loaded)'
terminating with uncaught exception of type NSException
(lldb)
,
必须这样做!
let vc = PlacesPlusScreenController()
self.navigationController?.pushViewController(vc,animated: true)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。