Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top