用Swift项目中的Storyboard加载Objective-C文件

huangapple go评论59阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月12日 20:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定