Storyboard ViewControllers中的多态性 [英] Polymorphism in Storyboard ViewControllers

查看:128
本文介绍了Storyboard ViewControllers中的多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在iPhone XCode故事板ViewControllers中实现多态性。



这是我的问题:
我的应用程序中的两个不同的ViewControllers正在转向常见的ViewController。



这个常见的ViewController的行为有所不同,具体取决于它所使用的VC - 执行不同的远程API服务器调用和CoreData提取(第一个呈现表中的所有用户,另一个呈现管理员列表 - 子类用户,我可能会有第三个会显示已过滤的用户列表。)



这个常见的ViewController在我的应用程序中是非常中心的,并且可以与许多其他ViewControllers一起使用。因此,我似乎没有权利使用子类在stroyboard上复制它。



遗憾的是,将用户列表传递给ViewController不是一个选择。在ViewController方法中执行和实现此列表上的不同搜索。



通过子类化和使用多态来实现这些不同的行为,并在故事板中分配ViewController是有意义的根据origin segue的子类。



有没有办法在segueing时动态设置Storyboard ViewController类?



$ b p>你在这里真正要求的是数据接口与视图接口不同。没关系。您应该提取一个负责获取数据的单独对象。您可以将该对象传递给视图控制器,而不是将视图控制器多态化。这被称为策略模式,在Cocoa中很常见。



这几乎与委托相同,您也可以在此处使用委托。 segue时,调用视图控制器在接收视图控制器上设置委托。然后该委托负责返回数据对象,再次使接收视图控制器免于多态化。


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?

解决方案

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.

这篇关于Storyboard ViewControllers中的多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆