Re: Subclassing a View Controller in a Storyboard
Re: Subclassing a View Controller in a Storyboard
- Subject: Re: Subclassing a View Controller in a Storyboard
- From: Dave <email@hidden>
- Date: Wed, 04 Sep 2013 22:27:47 +0100
On 4 Sep 2013, at 21:12, David Duncan <email@hidden> wrote:
> The point is, if the only difference between an LTWMusicViewController and an LTWRockMusicViewController is that the latter defaults to the “Rock” category, then you don’t actually *need* the LTWRockMusicViewController subclass. You just need to ensure that when you want an LTWMusicViewController created with the Rock category, that you ensure it is set as such.
>
> So in -prepareForSegue, you get the instance of LTWMusicViewController that is about to be transitioned to, set its category to Rock, and your done. No need for subclassing at all.
>
This code is in a method that takes the VC Class Name as a parameter, so, in order to do what you suggest I would beed code something like:
Normally the code reads:
myVC = [self.storyboard instantiateViewController: theViewControllerClassName]; //passed to this method.
This works fine for LTWMusicViewController, but if I want LTWRockMusicViewController, I need to add code to do this:
if ([theViewControllerClassName isEqualToString:@"LTWRockMusicViewController"] == YES)
{
myVC = [self.storyboard instantiateViewController:@"LTWMusicViewController"];
myVC.pMusicCategory = Rock;
}
else
myVC = [self.storyboard instantiateViewController:theViewControllerClassName]; //passed to this method.
return myVC;
Which is really messy since if I want a Blues, Jazz and HipHop Category, have to keep adding to the class names to check, which sucks! It's much better OOP to do the bits that are specific to LTWRockMusicViewController in the LTWRockMusicViewController class! Why should the initiating method need to know details of the VC's implementation?
Also, in this example I am showing only myVC.pMusicCategory, there could be any number of "special" things that LTWRockMusicViewController might want to do, that's why its overriding the base class!
Normally, if I wasn't using a Storyboard, I just do this:
myViewController = [[theViewControllerClassName class] initWithNib: theViewControllerClassName bundle:nil];
Which would take care of it nicely, since myViewController will be subclassed from LTWMusicViewController if theViewControllerClassName == LTWRockMusicViewController.
All I want to do is achieve the same thing using a Storyboard, but it's beginning to look like it's not possible?!?!?
All the Best
Dave
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden