无法在 Swift 包中展示 UIViewController 及其 Storyboard
Not able to present UIViewController with its Storyboard in a Swift Package
我正在构建一个单独的功能作为 swift 包。包中有一个视图控制器及其故事板。我在实例化和呈现视图控制器时遇到问题。我收到此错误:
Could not find a storyboard named 'MyIdCardVC' in bundle NSBundle ...
MyIdCardVC.swift 和 MyIdCardVC.storyboard 都添加到包中。
这是 Package.swift 声明的一部分:
let package = Package(
name: "IDCard",
platforms: [
.iOS(.v11)
],
products: [
.library(name: "IDCard", targets: ["IDCard"]),
],
我在我的应用程序中展示包的视图控制器是这样的:
import UIKit
import IDCard
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let vc = IDCard.MyIdCardVC.getStoryboardVC()
present(vc, animated: true, completion: nil)
}
}
“getStoryboardVC”是包中声明的静态扩展方法
public extension UIViewController{
public static func getStoryboardVC() -> UIViewController {
let storyboard = UIStoryboard(name: String(describing: self), bundle: nil)
return storyboard.instantiateInitialViewController()!
}
这里是 MyIDCardVC.storyboard 的样子:
我在实例化故事板的视图控制器时使用 Bundle.module
解决了这个问题。 Bundle.module
指的是包含包:
public extension UIViewController{
public static func getStoryboardVC() -> UIViewController {
let storyboard = UIStoryboard(name: String(describing: self),
bundle: Bundle.module)// key part is Bundle.module
return storyboard.instantiateInitialViewController()!
}
}
然后在应用程序中,我像这样显示包视图控制器:
@IBAction func openCard(){
let vc = IDCard.MyIdCardVC.getStoryboardVC() as! IDCard.MyIdCardVC
vc.personNo = "11111"
vc.personId = "8888888"
present(vc, animated: true, completion: nil)
}
然后在故事板中,指定模块并取消选中 Inherit Module From Target
我正在构建一个单独的功能作为 swift 包。包中有一个视图控制器及其故事板。我在实例化和呈现视图控制器时遇到问题。我收到此错误:
Could not find a storyboard named 'MyIdCardVC' in bundle NSBundle ...
MyIdCardVC.swift 和 MyIdCardVC.storyboard 都添加到包中。
这是 Package.swift 声明的一部分:
let package = Package(
name: "IDCard",
platforms: [
.iOS(.v11)
],
products: [
.library(name: "IDCard", targets: ["IDCard"]),
],
我在我的应用程序中展示包的视图控制器是这样的:
import UIKit
import IDCard
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let vc = IDCard.MyIdCardVC.getStoryboardVC()
present(vc, animated: true, completion: nil)
}
}
“getStoryboardVC”是包中声明的静态扩展方法
public extension UIViewController{
public static func getStoryboardVC() -> UIViewController {
let storyboard = UIStoryboard(name: String(describing: self), bundle: nil)
return storyboard.instantiateInitialViewController()!
}
这里是 MyIDCardVC.storyboard 的样子:
我在实例化故事板的视图控制器时使用 Bundle.module
解决了这个问题。 Bundle.module
指的是包含包:
public extension UIViewController{
public static func getStoryboardVC() -> UIViewController {
let storyboard = UIStoryboard(name: String(describing: self),
bundle: Bundle.module)// key part is Bundle.module
return storyboard.instantiateInitialViewController()!
}
}
然后在应用程序中,我像这样显示包视图控制器:
@IBAction func openCard(){
let vc = IDCard.MyIdCardVC.getStoryboardVC() as! IDCard.MyIdCardVC
vc.personNo = "11111"
vc.personId = "8888888"
present(vc, animated: true, completion: nil)
}
然后在故事板中,指定模块并取消选中 Inherit Module From Target