Pergunta

I want to achieve the following:

I have a nib which has a View Controller which is responsible for that view.

I want to write classes which inherit from this view controller and therefore share the same nib file as the base view controller but add additional specific code. I could just build a whole lot of functionality into the base view controller but then it gets messy; I really want to be able to have the same base structure with one nib and then have subclasses which add additional features.

The trouble I am having is in instantiating (in code) the subclasses using the base class's view.

I have tried [NSBundle nibWithNibName:...] and [[vc alloc] initWithNib:...] - they all give errors.

Do I set the file's owner to the base class? Do I set the view's custom view controller for the base class? How do I achieve this?

Thanks

Foi útil?

Solução

To set up outlets from the view controller to the view or subviews, you must set the File's Owner in the nib to the view controller's class.

The actual view controller can be a subclass of the class declared as the File's Owner in the nib. But then the outlets must still be in the superclass, or you won't be able to load using the other subclass.

So for example let me call the view controller SuperVC, Sub1VC, and Sub2VC. Then:

  • Define all needed outlets in SuperVC

  • Declare File's Owner in the nib as a SuperVC

  • Draw all outlet connections in the nib, including view of course

Now say:

Sub1VC* vc = [[Sub1VC alloc] initWithNibName:@"nibname" bundle:nil];

Or:

Sub2VC* vc = [[Sub2VC alloc] initWithNibName:@"nibname" bundle:nil];

They will both work.

Outras dicas

It sounds to me like you need to set the view outlet.

This is a seriously common mistake. Just control + drag from the File's Owner (the base view controller), to the top level view, and set the outlet of view.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top