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 17:08:12 +0100
On 4 Sep 2013, at 15:02, Matt Neuburg <email@hidden> wrote:
>
> On Sep 4, 2013, at 4:13 AM, email@hidden wrote:
>
>> Date: Wed, 04 Sep 2013 10:23:04 +0100
>> From: Dave <email@hidden>
>> Subject: Subclassing a View Controller in a Storyboard
>>
>> Hi All,
>>
>> If I am using a Storyboard that contains a view controller, LTWMusicViewController that I'd like to sub-class like so:
>>
>> LTWRockMusicViewController : LTWMusicViewController
>>
>> Then in the viewDidLoad method, do:
>>
>> -(void) viewDidLoad
>> {
>> [super viewDidLoad];
>>
>> self.pMusicCategory = kRockMusicCategory;
>> }
>>
>>
>> ----------------------------
>>
>> At present LTWMusicViewController is loaded using:
>>
>> myViewController = [self.storyboard instantiateViewController:@"LTWMusicViewController"];
>>
>>
>> I want LTWRockMusicViewController to use same NIB etc, as LTWMusicViewController, but just pre-set the self.pMusicCategory property to Rock.
>>
>> How do I do this using a Storyboard? I don't really want to copy all the controls for LTWMusicViewController into LTWMusicViewController.
>
> What you're doing has a lot of bad smells:
>
> * If I'm only going to call instantiateViewController, why am I using a storyboard here at all? If I use a .xib file instead, I can tell the view controller what .xib to use at its view nib, regardless of it's class.
This is just one place where it is instantiated normally you don't the base class is instantiated "as is", plus the App has a lot of other view controllers.
A bit like subclassing any other object really, why else would you subclass something unless to add or change the behaviour of the Base Class. In this case, the default is to display all music, but sometimes I want the same display but limited to RockMusic or whatever.
>
> * If the only difference between this object and that one is self.pMusicCategory, why am I bothering to subclass? Why not set the music category of the instance as soon as it is created?
The code is already written, I'm trying to adapt it, there is no way I want to pull apart a storyboard to do just that.
> Gosh, if I weren't using a storyboard, I could even have a designated initializer that takes a music category as a parameter.
Well, so would I, but who ever wrote this used a storyboard and I have to use it!
> * Even if you must use a storyboard, you can set an arbitrary variable in the resulting instance, using the user-defined runtime attributes.
At the moment, the viewDidLoad method in the base class sets the Category property to "ALL", I call this super class AND THEN set the Category to Rock, thus overriding the initial setting. I can' t see how setting this attribute will help? Or are you saying in the base class, detect if the Attribute is present and if not use ALL else use whatever it is set to?
At the moment, the code instantiates the VC and then overwrites the Category with Rock before it runs. This works ok, but rather than have the calling class have to set the category.
There is one method that deals with presentingViewController and it is passed the Class Name, at present, the code does this:
if ([theViewControllerClassName isEqualToString@"LTWRockMusicViewController"] == YES)
{
// Instantiate LTWMusicViewController and set property.
return;
}
// Instantiate theViewControllerClassName.
>
> * If all else fails, implement loadView. Now finding the view is up to you. You can keep the view in a .xib file even if you are getting the view controller from a storyboard (delete the view controller's view from the storyboard to indicate that you will supply it in some other way).
>
> m.
Not possible because it would break too much else!
Cheers
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