开发者

An easy, clean way to switch/swap views?

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?

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开发者_JS百科 the newest view to close all views up to the root).

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!

Thank you for your time!


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.

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.

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 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];
    }
}}

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).

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜