View Full Version : Subclassing a UIViewController subclass

Jan 12, 2009, 07:38 AM
I have a UIViewController subclass and a xib file who's owner is set to the former. Now I need to subclass the first UIViewController subclass to add some specific behavior. However when I try to push an instance of the UIViewControllers subclass-subclass on the UINavigationController my program stops.

Any ideea on what I could be doing wrong? Do I need different xib's for the two classes?


Jan 12, 2009, 08:00 AM
Hey MihaiD,

You will need to create another XIB with the second subclass as the File's Owner.

Let's say you have subclass B which is a subclass of A. You instantiate an instance of A. In this scenario you have NO access to any methods or properties declared in B.

However, if you do it the other way around, you instantiate an instance of B, then you have access to everything in B and A(unless you make stuff private...). Make sense?

So, maybe you want your file's owner in the first xib to be subclass B?

Jan 12, 2009, 08:23 AM
I don't think that's strictly correct. You should be able to use a xib that has as it's File's Owner the parent class, with a subclass so long as you don't have anything in that xib that refers to the subclass. I've done it and it does work.

Jan 12, 2009, 08:47 AM
I see what your saying. I think it is feasible as you initialize the file owner separately from the xib. What is the error your getting? Have you tried stepping through the process?

Luke Redpath
Jan 12, 2009, 05:36 PM
Out of interest, what exactly are you trying to do, and have you considered using composition to vary behaviour rather than inheritance?

Jan 13, 2009, 01:05 AM

I am not trying to refer to anything in the subclass' subclass. In fact that class is pretty much empty for now. What I'm getting is a NSInvalidArgumentException

@Luke Redpath

I'm not sure composition would help me. I'm trying to reuse most of the functionality from my first UIViewController subclass

Jan 13, 2009, 01:23 AM
OK, found the problem. My superclass was relying on a certain member parameter being set and I forgot to do that. It all works now.