简体   繁体   中英

UIViewController Won't Unload

I have been researching this question almost since I started learning to program for iOS (February 2012). I have read lots of posts on here which seem to be asking the same question and I have tried to implement the answers into my application to no avail. I am sure I am missing something simple.

I have a view switching test application. There are three views: orange, blue, yellow. It starts out on the orange view with two buttons - switch to blue view and switch to yellow view. When I click on switch to yellow view - the views revolve against a white background. The yellow view also has two buttons - switch to orange view and switch to blue view. When I click on switch to orange view the view flips back but instead of a white background the orange view is the background. How do I make that orange view not be there?

This is the code that I use to do the transition:

-(IBAction)switchToYellowView:(id)sender {

YellowViewController *myViewController = [[YellowViewController alloc]
                                         initWithNibName:@"YellowViewController"
                                         bundle:nil];

[UIView beginAnimations:@"flipview" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft 
                       forView:self.view cache:YES];

[self.view addSubview:myViewController.view];

[UIView commitAnimations];

}

From what I have read, I thought I needed to remove the orange view from the superview. But when I use

[OrangeViewController.view removeFromSuperView]

I get an error saying that "property view is not found on object OrangeViewController."

And if I switch from the yellow view to the blue view the orange view is still in the background. And then switching back to orange from blue, yellow is still in the background.

I read somewhere else, that I need to release these views also by adding

[myViewController release]

after I commit the animations. I thought that this was unnecessary when using ARC (which I am using). And when I do this, trying to go back to the orange view causes a Bad Access crash.

Perhaps I need to do something with rootViewController? I did set the OrangeView as a subview of "window" in the app delegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

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

return YES;

}

viewController2 is a pointer to the OrangeViewController.

Because of the first comment, I started to look around for switching out rootViewController - and found Ken Anderson's lecture notes about switching the rootviewcontroller programatically. I can get the yellow to change in, but it isn't animating.

The next two comments are leading me in other directions - might the best thing be to use a containercontroller - like UITabBarController? Except use my own buttons and never display a tab bar?

I can, of course, provide the entire application for perusal. Thank you in advance!

Try to use:

[self transitionFromViewController:currentViewController toViewController:firstViewNav duration: options: animations:^{
        } completion:^(BOOL finished){

        }];

Inside switchToYellowView:, you are adding the yellow view as a subview of your current view, which is the orange view. Thus if you try to remove the orange view with [OrangeViewController.view removeFromSuperView] (which probably isn't working because view isn't a public property of OrangeViewController), then you would be removing the orange view which has the yellow view as a subview, effectively removing both from the window. I think what you want to do inside switchToYellowView is something like this:

[self.view removeFromSuperview]; //remove the orange view
[window addSubview:myViewController.view]; //replace it with the yellow view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM