Reputation: 8303
I'm learning Objective-C, and ran into something which doesn't make sense to me.
When we have a view controller (VC) drawn in Storyboard, you can call this VC via code by entering a string into the Storyboard ID (click on the VC in Storyboard, in Utilities sidebar, Identity Inspector tab, Identity section) using the instantiateViewControllerWithIdentifier method.
In my example, I've drawn a standalone UIViewController in Storyboard and set Storyboard ID to "orderList". In the code, I can call this view by this code, Code1:
UIViewController *ordersVC = [self.storyboard instantiateViewControllerWithIdentifier:@"orderList"];
If I create a subclass of UIViewController called "SCVCOrders", and insert some code into it that modifies how it looks - like setting a title or changing the background color I thought I'd be able to use this code Code2 to combine the drawn VC with the code in SCVCOrders:
SCVCOrders *ordersVC = [self.storyboard instantiateViewControllerWithIdentifier:@"orderList"];
But it doesn't combine them. It only uses what is drawn in the Storyboard. So next, in Storyboard, I set the drawn VC's class to SCVCOrders. Now Code2 gives me the combined look, but so does Code1. My conclusion from this is that Storyboard is taking precedence over the instantiation code. Xcode doesn't care what class I code the view to be, it will just look at Storyboard for this. Is this correct?
I imagine it would be useful to have ability to layer my code on top of what I draw in Storyboard. Using my example, I'd like to leave my drawn VC as the generic class. And use Code2 to layer code on top of the drawn VC. Or how else can we achieve something like this?
Upvotes: 2
Views: 966
Reputation: 25740
instantiateViewControllerWithIdentifier
will return whatever class is stored in the storyboard. Storing the class returned by this method in a pointer with a different class type doesn't change what kind of object that it actually is.
Ie, if you have a fish and call it a tiger, it doesn't change the fact that it is really a fish.
Upvotes: 2