如何以编程方式在一个*视图控制器下设置多个自定义UIViewControllers?

时间:2023-01-23 00:08:57

Being new to Xcode and Objective-C I find it hard to get my head around the Interface builder and Objective-C when doing things that are following the basic pattern. I have created a subclass of UIViewController that I want to instantiate several times to make a grid with each row being controlled by an instance of this class. So there will be one root view controller (with navigation etc) that should include/genereate all the instances of the custom sub-viewcontroller.

作为Xcode和Objective-C的新手,我发现在执行遵循基本模式的事情时很难理解Interface构建器和Objective-C。我已经创建了一个UIViewController的子类,我想多次实例化一个网格,每一行都由这个类的一个实例控制。因此,将有一个根视图控制器(带有导航等),它应该包含/生成自定义子视图控制器的所有实例。

Now what would be the best way to do this? All examples I can find are about navigation, where one view should replace another, but I want to have all the viewcontrollers visible on the same "page". Do I need to create a nib file for the custom controller at all? I have also been thinking about using the UITableView somehow but inserting my custom viewcontroller in every row.

现在最好的方法是什么?我能找到的所有示例都是关于导航的,其中一个视图应该替换另一个视图,但我希望所有的视图控制器都可以在同一个“页面”上看到。我是否需要为自定义控制器创建一个nib文件?我一直在考虑以某种方式使用UITableView,但在每一行中插入我的自定义viewcontroller。

Any help greatly appreciated!

任何帮助非常感谢!

1 个解决方案

#1


0  

Apple's documentation recommends using one view controller per screen. It is possible to decompose your interface and use multiple view controllers on one screen if you have a good reason to do it, but Apple hasn't really designed their frameworks to support this, so you'll run into pitfalls if you don't know what you're doing.

Apple的文档建议每个屏幕使用一个视图控制器。如果你有充分的理由可以分解你的界面并在一个屏幕上使用多个视图控制器,但Apple并没有真正设计他们的框架来支持这一点,所以如果你不这样做,你会遇到陷阱知道你在做什么。

In this case, I question whether each row of your grid really needs its own view controller. I find it hard to imagine a case where this would be the best choice, although it's hard to say for sure without knowing more about your app. Some things to consider:

在这种情况下,我怀疑你的网格的每一行是否真的需要它自己的视图控制器。我发现很难想象这将是最好的选择,尽管如果不了解更多关于你的应用程序的话肯定很难说。有些事情需要考虑:

  • What is your custom controller doing? Is it mostly changing the visual appearance of its corresponding grid row? If so, perhaps it would be more appropriate to subclass the UIView itself.

    你的自定义控制器在做什么?它主要是改变其相应网格行的视觉外观吗?如果是这样,或许将UIView本身子类化更为合适。

  • If this object is really behaving as a controller and not a view, consider implementing it as a subclass of NSObject rather than subclassing UIViewController. The UIViewController for your screen can capture events and delegate them to the appropriate custom controller object, or your custom views can capture their own events and notify their associated controllers of those events directly using a delegate pattern.

    如果此对象实际上表现为控制器而不是视图,请考虑将其实现为NSObject的子类,而不是子类化UIViewController。屏幕的UIViewController可以捕获事件并将它们委托给相应的自定义控制器对象,或者您的自定义视图可以捕获自己的事件,并使用委托模式直接通知相关控制器这些事件。

  • If you're sure you have a valid reason to implement these objects as UIViewController subclasses, check out my answer to this question.

    如果您确定您有充分的理由将这些对象实现为UIViewController子类,请查看我对此问题的回答。

#1


0  

Apple's documentation recommends using one view controller per screen. It is possible to decompose your interface and use multiple view controllers on one screen if you have a good reason to do it, but Apple hasn't really designed their frameworks to support this, so you'll run into pitfalls if you don't know what you're doing.

Apple的文档建议每个屏幕使用一个视图控制器。如果你有充分的理由可以分解你的界面并在一个屏幕上使用多个视图控制器,但Apple并没有真正设计他们的框架来支持这一点,所以如果你不这样做,你会遇到陷阱知道你在做什么。

In this case, I question whether each row of your grid really needs its own view controller. I find it hard to imagine a case where this would be the best choice, although it's hard to say for sure without knowing more about your app. Some things to consider:

在这种情况下,我怀疑你的网格的每一行是否真的需要它自己的视图控制器。我发现很难想象这将是最好的选择,尽管如果不了解更多关于你的应用程序的话肯定很难说。有些事情需要考虑:

  • What is your custom controller doing? Is it mostly changing the visual appearance of its corresponding grid row? If so, perhaps it would be more appropriate to subclass the UIView itself.

    你的自定义控制器在做什么?它主要是改变其相应网格行的视觉外观吗?如果是这样,或许将UIView本身子类化更为合适。

  • If this object is really behaving as a controller and not a view, consider implementing it as a subclass of NSObject rather than subclassing UIViewController. The UIViewController for your screen can capture events and delegate them to the appropriate custom controller object, or your custom views can capture their own events and notify their associated controllers of those events directly using a delegate pattern.

    如果此对象实际上表现为控制器而不是视图,请考虑将其实现为NSObject的子类,而不是子类化UIViewController。屏幕的UIViewController可以捕获事件并将它们委托给相应的自定义控制器对象,或者您的自定义视图可以捕获自己的事件,并使用委托模式直接通知相关控制器这些事件。

  • If you're sure you have a valid reason to implement these objects as UIViewController subclasses, check out my answer to this question.

    如果您确定您有充分的理由将这些对象实现为UIViewController子类,请查看我对此问题的回答。