10

I have the same problem as Pedro here but not satisfied with the answer and since there is allready a bounty out and rewarded I created this question and I'll add a bounty as soon as possible.

I want to create or manipulate a segue (highlighted in yellow) in code such that the Master view is any one of a number of subclasses of MFMasterViewController (highlighted in red).

storyboard illustration

When doing this using Nibs I could create a Nib, SharedNib.xib & set the class as MFMasterViewController, then create my subclasses, say MFMasterViewControllerSubclassA, MFMasterViewControllerSubclassB etc. & then instantiate whichever subclass I wanted using...

MFMasterViewControllerSubclassA *controller = [[MFMasterViewControllerSubclassA alloc] initWithNibName:@"SharedNib" bundle:nil];

or...

MFMasterViewControllerSubclassB *controller = [[MFMasterViewControllerSubclassB alloc] initWithNibName:@"SharedNib" bundle:nil];

etc.

Any clues as to how I can get this right using storyboards?

I cannot use the provided answer on Pedro's question, my subclassing extends beyond the datasource & delegate.

Community
  • 1
  • 1
Tieme
  • 62,602
  • 20
  • 102
  • 156

2 Answers2

0

You can just add unconnected view controllers to your storyboard and give them identifiers. Then in code you can do something like this in your navigation controller subclass:

MFMasterViewControllerSubclassA *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"SubclassA"];
self.viewControllers = @[controller];

This will change the navigation controller's root view controller to controller. If you want to animate the change, then you could use setViewControllers:animated: instead of that second line.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Yes but each viewController can have only one identifier and one subclass. So it is not possible to get Subclass A and Subclass B with "instantiateViewControllerWithIdentifier" – Tieme Nov 29 '12 at 10:41
  • @Tieme, Don't you want to switch which class you want controller to be? Each subclass can have its own identifier, but you do need one disconnected view controller for each subclass that you're trying to instantiate (if you want it all done in IB). Is that what you're trying to avoid - having one scene for each subclass? – rdelmar Nov 29 '12 at 16:40
  • 8
    I want to avoid multiple scene's in IB, I want one scene in IB where I can derive multiple subclasses from with all connected outlets! – Tieme Nov 30 '12 at 12:33
0

I know this is an old post, but I thought I'd respond since it hasn't been answered yet. This is not difficult to do. Wherever you wish to refer to your superclass simply refer to it by the appropriate subclass. The subclass contains everything the superclass does. There are a number of ways to do this depending how you wish to do your segues. If you're using prepareForSegue then do it like this:

  1. Create a segue from a button or whatever.
  2. Assign it an id
  3. Import your subclass into the .m file
  4. In prepareForSegue do the following.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
       {
          if ([segue.identifier isEqualToString:@"YourID"]) {
                SubclassA *subA = segue.destinationViewController; // the file's owner in the storyboard is set to the super class
                subA.someString = @"subclass A is setting this";
            }
       }
    

This is pretty limited since you are sharing a storyboard scene and there is no subclass of the scene. You would have to hide elements, for instance, and then unhide them if it were instantiated using a particular subclass. Another way to do this is to set a boolean on the one class depending on where you're segueing from and then write conditional code. This latter way might be regarded as a bit of a code smell, but it's better than duplicating storyboards which is probably a really bad idea.

Nagarjun
  • 6,557
  • 5
  • 33
  • 51
SmileBot
  • 19,393
  • 7
  • 65
  • 62
  • 1
    Replying to old posts is always good! Duplicating storyboards or controllers in my storyboard was what I wanted to avoid indeed. I think I get what you're saying but I don't think that that is the solution I was looking for. However I haven't been working on this project for a while. I'll get back to this question when I pick it up again. In the meantime, anyone please upvote if this worked for you. – Tieme May 23 '14 at 08:22