Imad Ali
Imad Ali

Reputation: 3301

View Controller With two different XIB's..Tricky

I have a tricky question here..Please help..

I have One ViewController Called "DemoViewController" two different Xib's (Demo1Controller.xib & Demo2Controller.xib) are linked to the DemoViewController, Will load the Xib based upon the Condition..

I have navigation controller implemented in AppDelegate, Currently I am pushing this view controller(DemoViewController) with the XIB Demo1Controller, When User Taps a button in Demo1Controller, I need to load the same Viewcontroller i.e, DemoViewcontroller with Xib Demo2Controller..

Can this Possible?? Or Do i need to maintain the Different Classes for two Xib's

Let me know if you have any questions...

Upvotes: 0

Views: 97

Answers (3)

Leeloo Levin
Leeloo Levin

Reputation: 443

As a ViewController is just an object like any other object you can stack as many of them as you need on top of each other. Creating as many instances of that object as you want.

When you instantiate them you can do:

UIViewController *viewController = [[DemoViewController alloc] initWithNibName:@"Demo1Controller" bundle:nil];

or

UIViewController *viewController = [[DemoViewController alloc] initWithNibName:@"Demo2Controller" bundle:nil];

As long as the IBOutlets and delegate are set up correctly on both .xib's and they are set up using the same Custom class in IB. (Third icon from the left in the inspector panel, at the top.) If you fail to set them up properly it will simply crash on build and run.

And you can also check out a similar question I answered with a different approach some time ago. Another approach

Upvotes: 3

JoeFryer
JoeFryer

Reputation: 2751

I'm not sure if this is helpful to you, or appropriate for your situation, but I would probably suggest a slightly different approach (obviously, without knowing very much about your specific situation).

I would probably suggest that, rather than having one class have two different nibs, instead you have one class that has all of the common behaviour that these two 'screens' share, and then two concrete subclasses of this common parent class for each of the 'screens'. I am assuming that there are slight behavioural differences between the two.

You could then create an instance of your concrete subclasses with the specific nib name, as usual:

SubclassOneViewController *viewController = [[SubclassOneViewController alloc] initWithNibName:@"SubclassOneViewController" bundle:nil];

Upvotes: 0

Rajesh
Rajesh

Reputation: 10434

TRY THIS

- (id)init
    {
     if (YES)
                self = [super initWithNibName:@"VC1" bundle:nil];
            else
                self = [super initWithNibName:@"VC2" bundle:nil];

            return self;
    }

Upvotes: 0

Related Questions