Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the deleted xib gets loaded?

Tags:

ios

iphone

I have created a class 'abc' which a subclass of UIViewController. At the time of creating it i clicked the option for creating an xib for it automaticaly. Now the xcode creates 3 files for me 1. abc.h 2. abc.m 3. abc.xib.

Now whenever i create an object of abc class like abc *a=[abc alloc];

Even when i am not initialising the object with initiwithNibName and using it, it is loading the xib file. So how this xib file got associated with the abc object. And even if i deleted the abc.xib, then also it loads that xib file. I couldnt understand from where it is loading the xib file, if it it not present in the project space. And where the association of xib and controller is stored?

Thanks in advance.

like image 670
user1147070 Avatar asked Jan 13 '12 06:01

user1147070


2 Answers

What's going on is that the default implementation of initWithNibName:bundle: searches the Main Bundle for a Nib file that has the same name as your View Controller class. This happens whether you select the option for creating the Nib automatically or not. See UIViewController documentation (the discussion portion of initWithNibName:bundle:).

Now the initWithNibName:bundle: method is UIViewController's default initializer, which means that even if you don't use it directly (say that you use init instead) it will get called under the hood anyway.

Finally, even if you delete the Nib file from XCode, for some reason (not sure why) it doesn't get deleted from the Main Bundle (at least in the simulator). Even if you clean & build the project it stays there. The solution I use to get completely rid of the Nib file is to delete the App from the simulator, then clean & build again.

Hope this helps!

like image 64
LuisCien Avatar answered Nov 07 '22 19:11

LuisCien


The xib is probably still in your compiled area, so you need to perform a clean to get rid of it fully. (Product >> Clean). The default init method of UIViewControllers will automatically look for a xib of the same name, which is why it's still allocating that xib. Once you clean it will stop.

Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:

Note this part: If you specify nil for the nibName parameter, you must either override the loadView method and create your views there or you must provide a nib file in your bundle whose name (without the .nib extension) matches the name of your view controller class. (In this latter case, the class name becomes the name stored in the nibName property.) If you do none of these, the view controller will be unable to load its view.

like image 36
Philippe Sabourin Avatar answered Nov 07 '22 17:11

Philippe Sabourin