【问题标题】:Getting reference to separate bundle in code在代码中获取对单独包的引用
【发布时间】:2015-11-02 09:07:00
【问题描述】:

我们使用下面的代码来获取我们为 xib 和图像等资源添加的包的引用,但是我不确定在 swift 中访问它们的代码是什么。

NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:@"DTPinLockController" ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];

任何人都可以将其转换为 swift 1.2 代码吗?

【问题讨论】:

  • 你为什么做不到?
  • 我的意思是我没有 swift 等效代码。我想从中访问一个xib。我能够在目标 c 中做到这一点
  • 另一件事是 NSBundle.bundleWithPath 方法不可用
  • 你还没有读过任何关于 Swift 的文章吗?
  • 是的,我读过但是我没有经历过这些事情

标签: iphone xcode xib nsbundle


【解决方案1】:

Swift 中的构造函数与 Objective-C 略有不同。试试这个:

let path = NSBundle.mainBundle().pathForResource("DTPinLockController", ofType: "bundle")!
let bundle = NSBundle(path: path)

然后在该包内的NIB中加载UIViewController:

let controller = UIViewController(nibName: "MyViewController", bundle: bundle) as! MyViewController

查看Apple documentation 以获取完整参考。顶部有一个选择器,可让您在 Swift 和 Objective-C 之间切换。

【讨论】:

  • var bunPath:String = NSBundle.mainBundle().pathForResource("mybundle", ofType: "bundle")! let bundle = NSBundle(path: bunPath) bundle!.loadNibNamed("MyViewController", owner: self, options: nil) 它显示 nil 值为什么会这样
  • 这可能是因为主应用程序包中的myBundle.bundle 包中没有名为MyViewController.nib 的笔尖。
  • 添加了从 NIB 加载 UIViewController 的示例
  • mybundle.bundle 中有一个名为 MyViewController 的 xib,它也包含在复制包资源中
  • @redent84...捆绑包中有一张图片,我可以通过故事板中的 bundle.bundle/youtube.png 访问该图片。我怎么不能访问 xib 文件
猜你喜欢
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多