Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Set custom class name to storyboard programmatically

I am trying set custom class name to storyboard programmatically in iOS.

I have storyboard with viewcontroller MyFirstViewController and set class name as MyFirstViewController

Now I create another viewcontroller SecondViewController which extends MyFirstViewController

 SecondViewController *vcsecond = (SecondViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"MyFirstViewController"];
        [self.navigationController pushViewController:vcsecond animated:YES];

When I run this app my SecondViewController viewDidLoad method not call, it calls MyFirstViewController viewDidLoad method.

I get vcsecond as object of MyFirstViewController.

Please help me how to open SecondViewController using MyFirstViewController XIB.

I only want to inherit all the functionality of MyFirstViewController to SecondViewController and also override some method's here and execute some new code in overrided method with additional functionality.

Design of the both viewcontroller is same only add some new functionality using overriding method's.

Please refer this

In above screen TabSearchForDocViewController is my MyFirstViewController controller.

So my actual question is----------

I have comman storyboard page for two view controller. My SecondViewController is extends MyFirstViewController. Now I want to open my SecondViewController using comman storyboard page. Is it possible to set class name programmatically?

Upvotes: 7

Views: 952

Answers (2)

Paulw11
Paulw11

Reputation: 114773

instantiateViewControllerWithIdentifier will always return an instance of the class specified in the corresponding scene in the storyboard - you cannot override this at run time.

I would suggest that you create a 'thin' object which is the class that you specify in the storyboard scene. You can then create other object classes (using inheritance) that implement the view controller functionality using a delegation pattern. You can then instantiate the appropriate delegate instance at run time.

Upvotes: 2

Jasmeet Singh
Jasmeet Singh

Reputation: 564

I don't think that his will be feasible as your identifier "MyFirstViewController" holds the reference to the class MyFirstViewController and always loads the header files for this class. So I don't think that vcsecond would load for you this way.

Upvotes: 0

Related Questions