英文:
Loading obj-c files with storyboard in Swift project
问题
我是Swift世界的新手,已经创建了一个非常基本的应用程序,从名为Main
的故事板控制器加载了3个视图。
@IBAction func view2ButtonClicked(_ sender: Any) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: String(describing: Test2ViewController.self)) as? Test2ViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
}
@IBAction func view3ButtonClicked(_ sender: Any) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: String(describing: Test3ViewController.self)) as? Test3ViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
}
然而,我还复制了一个.h
和.m
文件,以及一个来自较旧的obj-c
项目的故事板。我已经设置了桥接头文件,并尝试使用以下方法加载它:
@IBAction func view1ButtonClicked(_ sender: Any) {
let storyboard = UIStoryboard(name: "CompassView", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "CompassViewController")
self.navigationController!.pushViewController(vc, animated: true)
}
但是它崩溃并显示以下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'CompassView' in bundle NSBundle
当我找到Compass故事板文件时,我检查了名称,它是Compass.storyboard
,自定义类是CompassViewController
,故事板ID是CompassView
。
英文:
I am new to the Swift world, and have created a very basic app that loads up 3 views from a storyboard controller called Main
@IBAction func view2ButtonClicked(_ sender: Any) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: String(describing: Test2ViewController.self)) as? Test2ViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
}
@IBAction func view3ButtonClicked(_ sender: Any) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: String(describing: Test3ViewController.self)) as? Test3ViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
}
However, I have also copied over a .h
& .m
as well as a storyboard from an older obj-c
project, I have setup the bridging header, and I am trying to load it using the following method;
@IBAction func view1ButtonClicked(_ sender: Any) {
let storyboard = UIStoryboard(name: "CompassView", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "CompassViewController")
self.navigationController!.pushViewController(vc, animated: true)
}
However it crashes with the error;
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'CompassView' in bundle NSBundle
When I locate the Compass storyboard file, I check the name and it is Compass.storyboard
and the custom class is CompassViewController
and storyboard ID is CompassView
答案1
得分: 2
当我找到Compass的故事板文件时,我检查名称,它是Compass.storyboard,然后您应该像这样加载它:
let storyboard = UIStoryboard(name: "Compass", bundle: nil)
英文:
> When I locate the Compass storyboard file, I check the name and it is Compass.storyboard
then you should load it like this
let storyboard = UIStoryboard(name: "Compass", bundle: nil)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论