问题
I want to be able to download (via a NSURLConnection request) a .XIB file, and have it presented in a view. I have implemented the NSURLConnection, and surely enough, when the connection completes, I am left with a NSString of XML data representing the XIB file. Example: (just the first few lines of many)
<?xml version="1.0" encoding="UTF-8"?>
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
<int key="IBDocument.SystemTarget">800</int>
<string key="IBDocument.SystemVersion">10D573</string>
<string key="IBDocument.InterfaceBuilderVersion">762</string>
<string key="IBDocument.AppKitVersion">1038.29</string>
<string key="IBDocument.HIToolboxVersion">460.00</string>
ET CETERA...
I looked through the documentation, and I believe that I cannot use "initWithNibName:" because it is being retrieved from the web. What I believe I need to do is save the .XIB to the app's document directory, and then use "loadNibFile:externalNameTable:withZone:"
Essentially what I want to do is have an iPad application, and there is a small 320x480 window, where a .XIB loaded from the web can be displayed. If I can attach it locally to a ViewController all the better, but not completely necessary.
I'm just not exactly sure how I should use "loadNibFile:externalNameTable:withZone:" Can anyone give me some advice or an example?
Thanks in advance!
回答1:
loadNibFile:externalNameTable:withZone: is an AppKit method, so it only works on Mac OS, not iPhone or iPad.
In SDK 3.2 you can use -[UIViewController initWithNibName:bundle:] or -[NSBundle loadNibNamed:owner:options:], depending whether or not you want the File's Owner to be a UIViewController. While you're right that you can't download a .xib and install it into your main bundle's resources, you can probably download a bundle, save it into your Documents directory, pass it to +[NSBundle bundleWithPath:], and then extract a .xib from it using initWithNibName:bundle: or loadNibNamed:owner:options.
I haven't tried this, but is seems like it should work. I'll be curious to know if it does.
I'll also be very interested to know if Apple accepts this app. It seems like this would put section 3.2.2 of the iPhone SDK Agreement to the test.
回答2:
You will not be able to do this with a plain .xib file, because those need to be compiled into a binary format for use within an actual application.
If you do compile the .xib files down into a proper iPhone .nib file, you might be able to place those within an external bundle and download that bundle to the application's Documents directory. You could then create an NSBundle instance using +bundleWithPath:
or the like, and pass the Nib name and bundle into a UIViewController's –initWithNibName:bundle:
.
来源:https://stackoverflow.com/questions/2618313/load-and-display-a-xib-from-the-web