Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The XIB, which is referred to as a NIB (as a matter of history), defines at least one view that is to be "controlled" by a view controller. This view can represent the whole user interface or simply a subview of another view (e.g. your XIB could represent a reusable table row). Thus, you <em>should not</em> be using a XIB as a sort of container for pre-built interface elements in the manner you describe.</p> <p>However, it is simple to work with the components of the XIB provided your controller knows about them. That is, the elements of your XIB should connect to properties of your view controller class.</p> <p>For example, let's say you have the following view controller interface:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; interface MyViewController : UIViewController { } @property (strong, nonatomic) IBOutlet UITextView *textEntry; @property (strong, nonatomic) IBOutlet UIButton *enterButton; </code></pre> <p>A corresponding NIB would be named <code>MyView.xib</code>. In the interface builder, you would set the "File's Owner" for the NIB to be "MyViewController". You would then link the interface elements, a UITextView and a UIButton, to MyViewController's properties (in whatever method you prefer - usually an option+click &amp; drag from the interface element to the File's Owner object).</p> <p>Having done this, you can then instantiate the view controller anywhere you please and work with the properties of that object. For example, let's pretend this code is in a file named "SomeOtherController.m":</p> <pre><code>- (void)aMethodOfSomeOtherController { MyViewController *myView = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nibBundleOrNil]; NSString *buttonLabelText = [[[myView] enterButton] titleLabel] text]; NSLog(@"Button label text = %@", buttonLabelText); [myView release]; } </code></pre> <p>When this method is invoked, an instance of <code>MyViewController</code> will be created which will automatically load the stored objects from the NIB and bind them to the view controller object's properties. It will then retrieve the text of the button's label and write it to the log console.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload