2

I am wondering how can I implement polymorphism in the iPhone XCode storyboard ViewControllers.

This is my problem: Two different ViewControllers in my application are segueing to a common ViewController.

This common ViewController is behaving differently depending on the VC it was segued from - different remote API server calls and CoreData fetches are performed (for the first it presents all users in a table, for another it presents a list of admins - subclass of users, and I may have a third one that will presents filtered list of users).

This common ViewController is very centric in my application, and segues to many other ViewControllers. Therefore, it does not seem right to me to duplicate it on the stroyboard with a subclass.

Passing the list of users to the ViewController is less of an option unfortunately, since different searches on this list are performed and implemented across the ViewController methods.

It makes sense to implement these different behaviours by subclassing and using polymorphism, and assigning the ViewController in the storyboard the subclass according to the origin segue.

Is there any way to set the Storyboard ViewController classes dynamically when segueing?

asaf am
  • 1,345
  • 15
  • 28
  • i don't see any reason to add a property in your common view controller to determine which uiviewcontroller you're segueing from then do the appropriate calls. – limon Jul 29 '13 at 13:46
  • @mstfbsnli I see your point, but I am looking for leaner classes and cleaner code solution. Am trying to avoid the if-else if-else conditionals on the different VC methods – asaf am Jul 29 '13 at 13:51

1 Answers1

2

different remote API server calls and CoreData fetches are performed

What you're really calling out here is that the data interface is different than the view interface. That's fine. You should pull out a separate object responsible for fetching data. You can pass that object along to the view controller, rather than polymorphing the view controller. This is called the Strategy pattern, and is very common in Cocoa.

This is almost identical to delegation, which you can also use here. When you segue, the calling view controller sets a delegate on the receiving view controller. That delegate is then responsible for returning the data objects, again freeing the receiving view controller from polymorphing.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • This answer is great, and I will implement this approach but I understand that in that same sense - there is no way to set the ViewController class dynamically. am I correct? – asaf am Aug 01 '13 at 11:12
  • There's certainly a way :) it's just not a good idea for this kind of problem. You can isa-swizzle the receiving class into a different class at runtime. But that would be a terrible idea here. Isa-swizzling can lead to really amazing bugs if you make any mistakes (like adding an ivar in the subclass; I made that mistake once). – Rob Napier Aug 01 '13 at 12:28
  • For more information: http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ManagingDataFlowBetweenViewControllers/ManagingDataFlowBetweenViewControllers.html#//apple_ref/doc/uid/TP40007457-CH8-SW4 – Rob Napier Aug 01 '13 at 12:33
  • got it. I will go with your approach. Refreshing my mind with the Strategy pattern seems to be the right solution. – asaf am Aug 01 '13 at 18:57