Krunal
Krunal

Reputation: 6490

Can I create 2 XIBs for one view?

Basically, what I want to do is,

I want to load 2 different XIB files on each click of Segmented control

Is it possible to load 2 different XIB files?

for eg:

When I click on View 1 my 1st XIB should be loaded and when i click on View 2 my 2nd XIB should be loaded.

Is it possible?

Upvotes: 1

Views: 261

Answers (3)

Nitin Gohel
Nitin Gohel

Reputation: 49730

you can this using xib like this bellow:-

  • Add two UIView or UIsegmentController and connect it's IBOutLate like bellow image .

enter image description here

  • In viewDidLoad secondView put as a hidden True like bellow:-

    - (void)viewDidLoad
    {   
        myview1.hidden=FALSE;
        myview2.hidden=TRUE;
    
        [super viewDidLoad];
    }
    

Put segment control ValueChange Action and connect with Segment control as a ValueChange:-

enter image description here

-(IBAction)segmentedControlIndexChanged{
    switch (seg.selectedSegmentIndex) {
        case 0:
            myview1.hidden=FALSE;
            myview2.hidden=TRUE;
            break;
        case 1:
             myview1.hidden=TRUE;
             myview2.hidden=FALSE;
            break;
               default:
            break;
    }
}

Now run you Project it's look like :)

enter image description here

Upvotes: 4

βhargavḯ
βhargavḯ

Reputation: 9836

If I understand your image, I can see two view within same xib. What you want to do is called load two different view from same xib.

you need to use [NSBundle loadNibNamed:owner:options:] method to achieve this.

NSArray *nib =   [[NSBundle mainBundle] loadNibNamed:@"MyXIBName" owner:self options:nil];

Array nib will hold all views under given XIB. You can iterate/filter, and then load desired view on segment control click.

Upvotes: 1

Suryakant Sharma
Suryakant Sharma

Reputation: 3960

you can create two view inside the same nib/xib file and add particular view on click Segemented control One view already has outlet in controller class and for other view create a IBOutlet and use.

Upvotes: 1

Related Questions