3

I want one UIViewController in my storyboard instanced by several class. So i let the field "class" empty in my storyboard (so by default, its UIViewController?). Then I fill storyboard ID with "MyGenericView".

Here is some class:

@interface ClassA: UIViewController

@interface ClassB: UIViewController

MyGenericView contains all prototype I need to build my view in ClassA and ClassB. Here is how I instancied my ClassA:

ClassA *myClass = (ClassA*)[storyboard instantiateViewControllerWithIdentifier:@"MyGenericView"];

Finally, my view is showed in the app but code in my ClassA is never called. Object returned by instantiateViewControllerWithIdentifier is an UIViewController, cast don't work.

I don't want to fill field "class" with ClassA because I want to reuse this view for ClassB. However I don't want to duplicate this view in my storyboard.

Dunno if it seems clear for you, I apologize for my bad english :)

Thanks!

Dilip
  • 8,873
  • 4
  • 40
  • 56
ApheX
  • 685
  • 2
  • 10
  • 24

3 Answers3

4

I blow my mind to find a solution to this question but the only thing that can work (i think) is to use delegates instead of subclassing.

I mean, you can implement 3 classes: ClassP, ClassC1, ClassC2; link your storyboard with ClassP that implements a delegate protocol, then before presenting (into prepearForSegue?) you can set the delegate of segue.destinationViewController (of P type) to one of your classes ClassC1 or ClassC2, that implements the protocol obviously.

Example Code for (not tested):

ClassP

@protocol ClassPDelegate <NSObject>

- (void)foo;

@end

@interface ClassP : UIViewController
...
@end

ClassC1 and ClassC2

#import "ClassP.h"
@property (nonatomic, weak) id <ClassPDelegate> delegate;

Caller Class

...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"mySegue"])
    {
        ClassC1 *classC1 = [[ClassC1 alloc] init];
        [(ClassP *)segue.destinationViewController setDelegate:classC1];
    }
}
...

I hope it works! ps: I'am sorry but your english is better then mine!

Alessandro
  • 351
  • 1
  • 2
  • 7
0

Only when I replaced almost anything in storyboard with xib files then My life becomes much easier than before!

Linc
  • 1,239
  • 11
  • 15
-1

What you want to do, and what are possible aren't always the same thing. Casting doesn't magically turn a UIViewController into a ClassA view controller. You need to change the class in the storyboard to ClassA or ClassB. You can copy and paste the view you've created into as many other view controllers as you want. What's wrong with that?

rdelmar
  • 102,832
  • 11
  • 203
  • 218
  • 5
    In my opinion this is not a nice solution. I'm facing this problem, too. When you want one base class with a defined UI and you have e.g. 10 subclasses that should have the same UI (or small changes that will done in code) it feels wrong to have 11 viewControllers in the storyboard. So you can't do nice OOP or inheritance. Shame on Apple! – Sebastian Feb 14 '14 at 19:29