4

I am just wondering is there a way to convert nib/xib file to ojbective C code? I just want to find the equivalent code to the nib/xib file (I've tried nib2objc, seems the result is not what I am after).

Actually I want to compile this example

http://developer.apple.com/iphone/library/samplecode/TableSearch/index.html

without nib/xib file (I want it exactly the same with original), any idea about doing this?

4
  • 1
    just out of curiosity, why do you want to do this? I've done quite a bit of iPhone development, and there aren't really many reasons to work without NIBs. Once you get used to Interface Builder, they make this stuff so much faster to develop... Commented Dec 25, 2009 at 5:56
  • I need to do this in some special cases. So I need the app performs exactly the same without NIB. Any idea about this? Commented Dec 25, 2009 at 6:09
  • are there cases where a NIB file might not be available? You can manually unserialize a NIB at runtime using NSBundle's loadNibNamed:owner:options:, so even if you have a funky, unconventional method of loading your interface you can still read in the views. Commented Dec 25, 2009 at 6:54
  • 1
    If you are new to iPhone development, you really should be using Interface Builder. Commented Dec 25, 2009 at 8:33

4 Answers 4

14

Check out nib2objc:

nib2objc converts NIB files (or XIB ones) into Objective-C code, including all the properties of each instance, the documented constructor calls, and also the view hierarchy.

nib2objc yourfile.xib > code.m 
Sign up to request clarification or add additional context in comments.

4 Comments

thank you, but I am new to iPhone dev. How do I use these code?
the code converted by nib2objc did not contain the scope buttons (for search bar).
If you're new to iPhone development then you probably shouldn't be trying to re-invent the proverbial wheel.
The link to nib2objc seems to be dead :-(
7

In the general case, I think the answer is no. Nibs are not code; they're archives of serialized objects. So what you're asking is, "Given a graph of arbitrary serialized objects, can I generate some source code that might create such a graph without using serialization?" Without special support for such a process in all classes involved, I don't see how you could.

It would probably be more beneficial to ask about what you actually need to accomplish rather than this specific way of doing whatever it is.

2 Comments

I agree. There shouldn't be any reason not to use a nib. Granted, if you're starting a new project, you may choose to write all your views in code instead of using IB, but if you already have XIBs, there's no reason to transform them into code and throw them away...
If you are creating localized apps then nibs have limited use. You can use them for layout, but much of the rest of the set up has to be done in code. Also, if you have particularly complex nibs with a lot of overlaying views, they become impossible to manage because there is no way to hide parts in IB. In these situations it is very useful to do some of the work in IB and then abandon it. Being able to convert nibs to code saves a lot of work.
1

To all the 'There shouldn't be any reason to not use a nib/xib' type responses here, let me provide a concrete example as a counterpoint.

[iPhone OS 3.1.3, 3.2]

  1. iPhone OS apps have a limited amount of memory to work with. If you use too much your app is automatically terminated by the OS.
  2. Interface builder loads images referenced in a nib/xib into a cache using [UIImage imageNamed...], which uses up memory and does not automatically release that memory. There is also no way for you to request those images in that shared cache be released.
  3. Lay out some large images in nib/xib files (i.e. - backgrounds), in an app with multiple nib/xib files, and you will very quickly have an app which gobbles up memory quickly (3mbs per full size png background on iPad), that you have no control over releasing. Any doubts about this, check the documentation for [UIImage imageNamed] and do some googling to verify that is what is used.

That's a case for not using a xib/nib.

1 Comment

Rebuttal: 1. Newer iPhone OS / iOS devices have more memory than the 3G and the 1st gen iPhone, so this problem is solving itself; 2. The [UIImage imageNamed:] bug you mention has been corrected in iPhone OS 3.0, it only concerns iPhone OS 2.x versions. Which is not supported anymore by Apple, and apps supporting iPhone OS 2.x are not allowed anymore in the App Store. 3. This is a problem regarding the obvious memory requirements of large images, not about NIB files per se. Disclaimer: I'm the creator of nib2objc, so my views are biased, and I use XIB/NIBs whenever possible and appropriate.
0

I think it makes sense to do this if you just want to create a starting point for a view that is created programmatically in a way that isn't easy or possible to do with Interface Builder. It is also convenient if you want to bundle the view hierarchy in to a static library so that it is fully self-contained. Ideally you want to use nibs if possible though. It will make internationalization easier. However you can still support internationalization with a strings file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.