jlmg5564
jlmg5564

Reputation: 1380

ios loadNibNamed to Superclass

I have got a view.xib file with a CustomClass = "BaseClass" (I configure it on XCode editor), so I can do that:

BaseClass *mBassClass= [[[NSBundle mainBundle] loadNibNamed:@"view" owner:self options:nil] objectAtIndex:0];

But I want to know how I can do that:

I would have got a child class from BaseClass. For example ChildClass extends from BaseClass, and load a view from Nib but with a ChildClass

ChildClass *aChild = [[[NSBundle mainBundle] loadNibNamed:@"view" owner:self options:nil] objectAtIndex:0];

ChildClass2 *anotherView = [[[NSBundle mainBundle] loadNibNamed:@"view" owner:self options:nil] objectAtIndex:0];

So, I want to have got BaseClass with some child class with specific functions but all of these views have got the same Nib file

In my case I'm developing the following views:

(1) A nib file, it has got some uibutton from control and a UIView that will contain a Core Plot Graph

(2) A BaseClass, called GraphView, it matchs with the nib file and it does function with the uibuttons.

(3) Some child class, each one paint a different graph/plot on the UIView, but the inherits the functions of uibutton controls.

Any suggestion? Thanks

Upvotes: 1

Views: 838

Answers (1)

Wain
Wain

Reputation: 119031

Create your XIB as the container for the graph. It contains all of the common views and functionality, but not the graph specific content.

Now, when you load the NIB, you will always get a BaseClass instance. Indeed, you must always get a BaseClass - you can not change the class that is unarchived from the NIB.

Your graph views shouldn't be subclasses of BaseClass. Your graph view class that is appropriate to the current situation should then be instantiated and added as a subview of the container view, which is owned by the BaseClass (container) instance.

If the graph view needs to change the common button status then you should provide the graph view with a reference to the BaseClass instance when you add it as a subview.

Upvotes: 2

Related Questions