一个ViewController与许多ViewControllers(One ViewController vs. many ViewControllers)

编程入门 行业动态 更新时间:2024-10-22 05:14:22
一个ViewController与许多ViewControllers(One ViewController vs. many ViewControllers)

我正在构建一个由不同视图组成的应用程序,这些视图彼此密切相关。 到目前为止,我只有一个UIViewController来控制这些不同的视图。 例如,视图1和2共享相同的背景,视图1和2之间的过渡是自定义动画。

我的问题是视图1和2都有一个UIScrollView。 我的UIViewController是他们的委托,我可以使用以下scrollViewDidScroll来区分两个scrollviews:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == 1) //handle a else if (scrollView.tag == 2) //handle b else if (scrollView.tag == 3) //handle c }

滚动视图1发生了很多,滚动视图2发生了不同的事情,代码将变得非常混乱。 理想情况下,我想在一个单独的文件中定义滚动滚动等会发生什么。但是我不希望有另一个UIViewController,因为转换变得更加困难。 我没有NavBar或ToolBar,所以UINavigationController和UITabBarController在我的情况下都不会很好用。

我该怎么办?

我在这里发了一个类似的问题。

I'm building an app which consists of different views which are closely related to each other. So far, I only have one UIViewController which controls these different views. View 1 and 2 share the same background, for instance, and the transition between view 1 and 2 is a custom animation.

My problem is that both view 1 and 2 have an UIScrollView. My UIViewController is their delegate and I could have the following scrollViewDidScroll to distinguish between the two scrollviews:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == 1) //handle a else if (scrollView.tag == 2) //handle b else if (scrollView.tag == 3) //handle c }

As a lot happens with scrollView 1 and different things happen with scrollView2, the code will become really messy. Ideally, I'd like to define in a separate file what happens if scrollView1 is scrolled etc.. Yet I don't want to have another UIViewController as then transitions become more difficult. I don't have a NavBar or ToolBar, so neither UINavigationController nor UITabBarController would work very well in my case.

What should I do?

I posted a similar question here.

最满意答案

如果您不想要两个视图控制器,只需为每个滚动视图创建一个单独的委托。 使它成为一个符合UIScrollViewDelegate的NSObject ,并在滚动视图的同时创建它。

似乎要组合您寻求的结果:一个视图控制器,但封装的滚动视图代码。

If you don't want two view controllers, just create a separate delegate for each scroll view. Make it an NSObject which conforms to UIScrollViewDelegate and create it at the same time as the scroll view.

Seems to combine the results you seek: one view controller, but encapsulated scroll view code.

更多推荐

本文发布于:2023-07-31 01:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ViewController   ViewControllers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!