Search code examples
iosxcodeuiviewcontrollerxib

Rename XIB file without loosing outlet connection


First, I've made the choice to not work with storyboards, as I'm quite used to working only with code (I'm from Java). So here is the behaviour I don't understand :

1/ I create a .xib file containing a view and, lets say a button.

2/ I create a ViewController (as i understood : one xib file containing many subviews is associated with one viewController)

3/ I set the file owner of my xib to the viewcontroller class

4/ I make the outlet connection between the button and the ViewController by the drag and drop "magic" of xcode.

Here everything works fine : at the beggining in my ViewDidLoad(), the outlet is set as a UIButton when i run the app, and i can use it

5/ I rename the xib file

6/ The outlet is nil when i run the app at the begginning of ViewDidLoad(). That is the problem.

I dont understand why. The file's owner of the xib is still the same, its still my viewcontroller.swift (i didnt rename the viewcontroller class but only the xib), so why this viewcontroller cant see what is his view by searching what xib it owns now ?? I tried to clean the project, close it, even restart my laptop nothing changed.. I may have missed something because its not normal that i cant rename a xib file without having to recreate another viewcontroller file ?

I'm definitely not used to work with graphical interface, and i was glad to see many developpers are not too. Can you advise me some tips to create a app without storyboards, using only xib like "HTML" files and everything else in the code ?

Thank you


Solution

  • The custom class of the File's Owner placeholder in the xib has only one effect: it tells Interface Builder (the xib editor part of Xcode) what outlets and actions to offer on the File's Owner placeholder. The File's Owner's custom class does not affect how your view controller finds its nib file at runtime.

    (A xib file is an XML-based document you edit in Interface Builder; a nib file is the compiled version of the xib file and is included in your app bundle.)

    The UIViewController.nibName documentation tells you what you need to know:

    This property contains the value specified at initialization time to the initWithNibName:bundle: method. The value of this property may be nil.

    If you use a nib file to store your view controller's view, it is recommended that you specify that nib file explicitly when initializing your view controller. However, if you do not specify a nib name, and do not override the loadView method in your custom subclass, the view controller searches for a nib file using other means. Specifically, it looks for a nib file with an appropriate name (without the .nib extension) and loads that nib file whenever its view is requested. Specifically, it looks (in order) for a nib file with one of the following names:

    1. If the view controller class name ends with the word ‘Controller’, as in MyViewController, it looks for a nib file whose name matches the class name without the word ‘Controller’, as in MyView.nib.

    2. It looks for a nib file whose name matches the name of the view controller class. For example, if the class name is MyViewController, it looks for a MyViewController.nib file.

    So, if you have renamed your xib, and now the outlet is nil, then your view controller is probably not loading the nib file at all, because of one or more of these reasons:

    1. You passed a nib name to the UIViewController.init(nibName:bundle:) initializer, and that nib name is incorrect.

    2. You passed nil to the UIViewController.init(nibName:bundle:) initializer, and none of the naming rules described in the nibName documentation matches your renamed xib file name.

    Here are some possible solutions:

    1. Pass your renamed xib file name (without the .xib extension) to the UIViewController.init(nibName:bundle:) initializer.

    2. Override the nibName property in your UIViewController subclass to return the renamed xib file name (without the .xib extension).

    3. Rename the xib file to match one of the rules described in the nibName documentation.