简体   繁体   中英

Objective-c - Getting objects from XIB file into a view controller

So I have a XIB file, which contains a view. In that view, I want to add custom objects and then being able to get those objects, and use them in a view controller I have where I crate other things programmatically .

My XIB file is called "MyXibLibrary.xib" My view controller where I want the objects to be added to is called "ContactDetailsViewController"

My view controller is being pushed from a UITableViewController like this:

 ContactDetailsViewController *detailViewController = [[ContactDetailsViewController alloc] init];

And inside my ContactDetailsViewController viewWillAppear I have this code to get the XIB objects:

 UIView *xibView = [[[NSBundle mainBundle] loadNibNamed:@"MyLib" owner:self options:nil] objectAtIndex:0];
    [self.view addSubview:xibView];

Now, a for instance, the loadNibNamed property should be? Name of the XIB file? Name of the view in that XIB file? Or what?

All this is bringing me errors and the app trows exeption.

I have no clue what so ever on how to work with XIB files since I super new to Objective-c coding.

Any help would be really appreciated!!! Thanks!

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 (eg your XIB could represent a reusable table row). Thus, you should not be using a XIB as a sort of container for pre-built interface elements in the manner you describe.

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.

For example, let's say you have the following view controller interface:

#import <UIKit/UIKit.h>

interface MyViewController : UIViewController { 
}

@property (strong, nonatomic) IBOutlet UITextView *textEntry;
@property (strong, nonatomic) IBOutlet UIButton *enterButton;

A corresponding NIB would be named MyView.xib . 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 & drag from the interface element to the File's Owner object).

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":

- (void)aMethodOfSomeOtherController
{
  MyViewController *myView = [[MyViewController alloc] 
                              initWithNibName:@"MyViewController" 
                              bundle:nibBundleOrNil];

  NSString *buttonLabelText = [[[myView] enterButton] titleLabel] text];
  NSLog(@"Button label text = %@", buttonLabelText);

  [myView release];
}

When this method is invoked, an instance of MyViewController 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.

If you look at the documentation for UIViewController , there's a method called initWithNibName:bundle: . It lists five different sample code projects that Apple provides to demonstrate how to use xib files and view controllers. You should read a few of those to get a basic understanding.

If you want to load an UIView from NIB, this code would be more correct (since you don't know the index of a needed object in the xib file).

- (id)loadViewFromNIB:(NSString *)nibName owner:(id)owner class:(Class)_class
{
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];

    for (id object in objects) {
        if ([object isKindOfClass:_class]) {
            return object;
        }
    }
}

Open MyLib.xib in interface builder and check that you actually have a UIView component in that file.

What error message do you get from the exception?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM