一种简单,干净的切换/交换视图的方法? [英] An easy, clean way to switch/swap views?

查看:76
本文介绍了一种简单,干净的切换/交换视图的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几个来源,但我仍然很困惑!我想创建一个具有多个视图的应用程序(只是标准视图,没有表视图或任何东西),我可以从中点击每个视图上的按钮来访问其他视图。

I have looked at several sources but I am still very confused! I want to make an application with several views (just standard views, no table views or anything), from which I can click buttons on each view to access the others.

我已经看到了几种方法,但是唯一对我有意义的方法是让应用程序委托负责交换视图。而且我甚至不确定我的代码是否正确清理了它正在交换的视图的内存!

I've seen several ways to do this, but the only one that made any sense to me is to have the application delegate be responsible for swapping views. And I'm not even sure my code correctly cleans up the memory of the view it's swapping!

是否有更简洁的方法在视图之间切换(我不喜欢关心动画比使用代表吗?

Is there a cleaner, or easier way to switch between views (I don't care about animations) than using a delegate?

我也看过presentModalViewController,但这看起来有点无组织。 (例如,如果您有多个视图呈现模态,您如何轻松地告诉最新视图关闭所有视图到根)。

I've also seen presentModalViewController, but that seems sort of unorganized. (E.g. If you have more than one view presenting a modal, how can you easily tell the newest view to close all views up to the root).

还创建了一个基于窗口的应用程序并使用窗口来添加addSubView和removeFromSuperview,但是如何在委托方法中创建额外的,繁琐的编码以检测删除的视图以及应添加哪个新视图时,如何将其中的一个交换为另一个?我至少找不到addViewToSuperview方法。

There's also creating a window-based application and using the window to addSubView, and removeFromSuperview, but how would you swap one very for another without creating extra, cumbersome coding in the delegate method to detect what view what removed, and which new view should be added? I can't find an addViewToSuperview method, at least.

所以请分享你们如何交换视图,以及对你的系统的好处。

So please share how you guys swap views, and perhaps the benefit to your system.

说话真的很慢,因为在交换视图时我觉得很慢!

Talk really slow, because I feel very slow when it comes to swapping views!

感谢您的时间!

推荐答案

如果您了解视图背后的概念,处理多个视图非常容易。例如,您的Delegate可以链接到空父视图。然后,您只需创建子视图,并使用 addSubview 将其添加到父视图中。最后添加的视图是将在前台显示的视图。

Dealing with multiple views is pretty easy if you understand the concept behind the views. For example your Delegate could be linked to an empty parent View. Then you just create your children views and add them to the parent view with addSubview. The last added view is the one that will be displayed in the foreground.

现在,您可以使用 bringSubviewToFront 方法将视图置于最前面。请注意,您的所有观看次数都会保留在内存中,直到您使用子视图的 removeFromSuperview 方法将其从父视图中删除。

Now you can bring the views into the front using bringSubviewToFront method. Keep in mind that all your views are kept in memory until you remove them from the parent view using the children view's removeFromSuperview method.

交换视图只是在添加下一个视图之前删除子视图,但是你必须决定是否要保留它们,或者如果你想要释放它们,那么你需要* 重新创建(alloc / init)以后的观点。

Swapping views is just removing the child view before you add the next one but then you have to decide if you want to retain them or if you want to release them but then you need to *re-create (alloc / init) the views later.

BTW如果您的观点不在正确的位置,那么您需要调整相对于父视图的位置或将它们设置在界面构建器,因为iOS不会重新定位它们,因为您将它们添加为子视图。

BTW If you views are not at the right place then you either need to adjust the positions relative to the parent view or set them right in the interface builder because iOS will not reposition them because you add them as sub view.

例如,我使用该代码在显示广告或代码的视图之间切换:

For example I used that code to switch between a view that displays an Ad or a ticker:

- (void) showAds:(BOOL)flag {
if( showAds != flag ) {
    while([[self.bottomView subviews] count] > 0 ) {
        [[[self.bottomView subviews] objectAtIndex:0] removeFromSuperview];
    }
    showAds = flag;
    if( showAds ) {
        [self.tickerViewController stop];
        [self.bottomView addSubview:self.adViewController.view];
    } else {
        [self.bottomView addSubview:self.tickerViewController.view];
        [self.tickerViewController start];
    }
}}

第二行确保我不删除并添加,如果没有任何改变。接下来的3行删除任何残留的子视图。之后,将Add或Ticker视图添加为我当前的子视图(包括确保正确启动/停止代码)。

The 2nd line makes sure I don't remove and add if nothing has changed. The next 3 lines remove any residual sub views. After that either add the Add or the Ticker view as my current sub view (including making sure the ticker is started / stopped properly).

希望有所帮助。

这篇关于一种简单,干净的切换/交换视图的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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