user2408952
user2408952

Reputation: 2041

iOS, How to load a .xib into another .xib?

I'm looking for a way to reuse content from one .xib file and load that into multiple other .xib files. Reason why is so i can reuse part of my design for all pages in my app. I know that in android programming, you can do this by writing include nameOfFile in your design .xml file.

I've tried the following:

Created a .xib file linked to a .h and .m file, they are all called ActionBarViewController .xib/.h/.m.

After that i set the backgroundcolor in my ActionBarViewController.m file.

self.view.backgroundColor = [UIImage gbBlue]; 

My .h for ActionBarViewController looks like this:

@interface ActionBarViewController : UIViewController {

}
@property (retain, nonatomic) IBOutlet UIView *actionBarView;

@end

Then i included my ActionBarViewController in my mainMenu.m (thats the .m file to the .xib i want load my ActionBar view into).

Now in my mainMenu.m's viewDidLoad i tried loading it like this.

ActionBarViewController *actionbarViewController = [[ActionBarViewController alloc] init];

[self.view addSubview:actionbarViewController.actionBarView];
[actionbarViewController release];

I've also tried:

ActionBarViewController *obj=[[[NSBundle mainBundle] loadNibNamed:@"ActionBarViewController" owner:self options:nil]objectAtIndex:0];

obj.actionBarView.frame=CGRectMake(70, 160, 180, 80);

[self.view addSubview:obj.actionBarView];
[obj release];

But its not working, so am i missing something? Or is this not even possible in iOS programming?

Upvotes: 2

Views: 3497

Answers (3)

Deep Gami
Deep Gami

Reputation: 508

  1. Simple you have to just create object of second xib and add in main view,like below:

    SecondXib obj=[[[NSBundle mainBundle] loadNibNamed:@"SecondXib" owner:self
    options:nil]objectAtIndex:0];
    
    obj.frame=CGRectMake(70, 160, 180, 80);
    
    [self.view addsubview:obj];
    

Note : You have to use secondXib as UIView

Upvotes: 1

Puneet
Puneet

Reputation: 301

if you have a mainmenu's xib, and you are trying to load in a specific view defined in that xib. lets name it as containerView. Link that containerView with your main .m file init your ActionBarView in the viewDidLoad. Use some class method in ActionBarView to load the nib file and return the results. Than add that as a subview in your containerView.

//viewDidLoad
    ActionBarView *actionBar = [ActionBarView getInstance];
    [containerView addSubview:actionBar]

//Class method getInstance in ActionBarViewController
    ActionBarView *actionBar = [[[NSBundle mainBundle] 
                                          loadNibNamed:@"ActionBarViewController" 
                                          owner:nil options:nil] lastObject];
    if ([actionBar isKindOfClass:[ActionBarView class]])
        return actionBar;
    else
        return nil;

ActionBarView is a UIView

Upvotes: 1

Apurv
Apurv

Reputation: 17186

You are creating controller by below way:

ActionBarViewController *actionbarViewController = [[ActionBarViewController alloc] init];

This is not proper if you want to init the controller via xib. Correct way is:

ActionBarViewController *actionbarViewController = [[ActionBarViewController alloc] initWithNibName:@"ActionBarViewController" bundle:nil];

Upvotes: 0

Related Questions