2015-11-02 25 views
0

我們使用下面的代碼來獲取對我們爲xib和圖像等資源添加的包的引用,但我不確定在swift中訪問它們的代碼是什麼。獲取對代碼中單獨的包的引用

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

任何人都可以將其轉換爲swift 1.2代碼?

+0

爲什麼你不能這樣做? – trojanfoe

+0

我的意思是我沒有快速的等效代碼。我想從中訪問一個xib。我能夠做到這一點客觀c – Saty

+0

另一件事是NSBundle.bundleWithPath方法不可用 – Saty

回答

0

Swift中的構造函數與Objective-C有點不同。試試這個:

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

以及在筆尖之後加載UIViewController這束裏面:

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

看看在Apple documentation爲完整的參考。頂部有一個選擇器,可以讓你在Swift和Objective-C之間切換。

+0

var bunPath:String = NSBundle.mainBundle()。pathForResource(「mybundle」,ofType:「bundle」)! 讓包=一個NSBundle(路徑:bunPath) 束.loadNibNamed(「MyViewController」,店主:自我,選擇:無)! 其顯示零值爲什麼這麼 – Saty

+0

這可能是因爲沒有名爲'MyViewController.nib'在筆尖主應用程序包中的'myBundle.bundle'包。 – redent84

+0

新增加載來自NIB的'UIViewController'的示例 – redent84