Understanding UIViewController hierarchy

一曲冷凌霜 提交于 2019-12-12 10:00:18

问题


Ok - my brain is being fried at the moment so any help would be appreciated.

I have multiple subclasses of UIViewController in my app. lets call them VC_A, VC_B, VC_C, VC_D.

The users interacts by touching buttons on each of the views.

So my AppDelegate adds in VC_A:

//Add the view controller's view to the window and display.  
[self.window addSubview:viewController.view];  
[self.window makeKeyAndVisible];  

VC_A then loads VC_B by using presentModalViewController:

    VC_B *tempView = [[VC_B alloc] initWithNibName:@"temploadingscreen" bundle:nil];
    [self presentModalViewController:tempView animated:NO];
    [tempView release];  

and so until I get a hierarchy of

VC_A 
- VC_B  
    - VC_C  
       - VC_D  

but then when I call presentModalViewController on VC_D to take me to VC_C I want it to be a new instance of VC_C and not the original instance.

So my question is how to you go about doing this - do I need to use [self dismissModalViewControllerAnimated:NO]; to remove the old instances of the views.

Any help would be gratefully appreciated as I have done searches for this but all the tutorials and stuff use a navbar to control the navigation - and i cant use one because of the type of app. Any working code examples of properly moving between new instances of UIViewControllers would be great.


回答1:


Just create a new instance with

ViewController_C *newVC_C = [[ViewController_C alloc] init]
[self presentModalViewController:newVC_C animated:NO];
[newVC_C release];



回答2:


I decided to do this a different way which works perfectly for what I need.

What I did was I created the base ViewController with nothing in the xib and in the viewDidAppear method I called the other viewControllers (using presentModalViewController) based on the value of a global NSNumber.

Thus when I go to any of the other viewcontrollers rather than them call another viewController they simply set the global variable indicating which view to load and then close the current view (using dismissModalViewController).

This way each instance of the viewControllers are closed and the memory released.

I have created an example project and placed it on github https://github.com/sregorcinimod/Open

Just look in the Downloads you'll see it there



来源:https://stackoverflow.com/questions/6418383/understanding-uiviewcontroller-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!