开发者

Iphone UIView switching: Previous view's subviews polluting newly switched-to view

I've been working for a while now on switching UIViews, and am trying a basic UIView switch, without using UINavigationController. I've looked into UINavigationControllers, but I have been wrestling with the below for so long that I want to explore it fully.

My project organisation is:

MainViewController.h/.m    -> viewDidLoad: This method runs a NSTimer to update self.view with a few bits and bobs.

ConfigView.h/.m: -> viewDidLoad: nothing - just an idle view.  

MainViewController.xib     -> Main window NIB

ConfigView.xib   -> ConfigView NIB

I have the following method in MainViewController.m execute on a UIButton press event:

- (IBAction)switchToConfigView:(id)sender {

if(self.configViewController == nil) {
    ConfigView *cv = [[ConfigView alloc] initWithNibName:@"ConfigView" bundle:nil];
    self.configViewController = cv;
    [cv release];
}

[self.view addSubview:configViewController.view];

}

Now, when I switch to the ConfigView, the view is indeed displayed, but it appears that viewDidLoad: from MainViewController still executes, causing both views to effectively be merged. From the code, I can see that this is obviously the case, as the actual view switch itself is performed within the MainViewController context, and as you can see, simply a subview.

My question is, how do I neatly tuck away/pause all goings-on within MainViewController when switching the view by just adding another subview to it? My guess is that it's not possible, or rather it's a lot of leg work where some other methodology would be better applied. I suspect I need to implement the actual switch from the ConfigView.h/.m but I could be wrong.

Any ligh开发者_运维知识库t you can shed on the above would be grand.

thanks

swisscheese.


My suggestion is:

1) have a base, empty view;

2) add a subview to it; this would be your actual first view;

3) when you want to switch, remove the first subview from the base view and add the second one as a subview;

4) when you want to switch back, remove the second one and add the first one as a subview to the base view;

you can easily handle as many view as you like. it works.

alternatively, you can hide the view instead of removing it.


Believe me adding a whole new sub-view on another view is really a massive task and later on when the project becomes big it becomes a painful task to switch views.

This is my own personal experience as I did this big mistake in one of my previous project.

In all cases navigation control is the best choice for switching views rather than adding sub-views.

Please tell me that what difficulties are you facing in Navigation control and I can help about this.

Thanks,


If the NSTimer you've set up in MainViewController's viewDidLoad method is adding subview's to mainViewController's view with the addSubview method, those views will be placed on top of all other subviews, i.e. on top of the config view. So, you might try either invalidating the timer when presenting the config view.

You should really just use a UINavigationController, or present the config view controller with presentModalViewControllerAnimated. Why don't you want to?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜