如何跨视图控制器共享 UIView 和其他元素?
我有一个应用程序,其中有一个 UINavigationController,其中有几个 UIViewController。每个 ViewController 都有自己的 nib。
这两个视图控制器 90% 相同。所以目前来说,存在大量的代码重复。所以我想知道有没有办法可以共享处理每个笔尖的代码?
换句话说,一个可以在视图控制器之间传递的类,可以管理所有界面元素?
I have an app which has a UINavigationController, which has a couple of UIViewControllers. Each ViewController has its own nib.
The two view controllers are 90% identical. So currently, there is a lot of code duplication. So I'm wondering is there a way I can share the code that deals with each nib?
In other words, a class that can be passed between the view controllers, that can manage all the interface elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会考虑使用单个 UIViewController 类,因为它们非常相似。在 init 方法中,只需让它加载该实例所需的笔尖即可。
另一种选择是让其中一个 UIViewController 扩展另一个。
不管怎样,代码越少通常越好。
You might consider using a single UIViewController class, since they are so similar. In the init method, just have it load the nib you need for that instance.
Another option would be for one of the UIViewController to extend the other.
Either way, less code is usually better.
我见过一个例子,其中每个 ViewController 继承自一个类,该类具有另一个类作为属性,该类是主类,该类(主类)具有两个视图所需的委托。
I've seen an example where each ViewController inherits from a Class wich has as property another class, wich is the main class, this class (the main) has the delegates needs for both views.