0

I tried to search this site and all over the internet, but I haven't found a way that will let me switch between multiple view controllers while sharing data between them. I tried to use the following code (the first method calls another method then switches view):

-(IBAction) choosePhoto:(id)sender {
[self startMediaBrowserFromViewController: self
                            usingDelegate: self];
if(firstViewController.view.superview == nil)
{
    [secondViewController viewWillAppear:YES];
    [firstViewController viewWillDisappear:YES];

    [firstViewController.view removeFromSuperview];
    [self.view insertSubview:self.secondViewController.view atIndex:0];

    [firstViewController viewDidDisappear:YES];
    [secondViewController viewDidAppear:YES];
}

//3. now add animation
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//blue view will appear by flipping from right
if (secondViewController.view.superview == nil)
{
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight 
                           forView:self.view cache:YES];

    [secondViewController viewWillAppear:YES];
    [firstViewController viewWillDisappear:YES];

    [firstViewController.view removeFromSuperview];
    [self.view insertSubview:self.secondViewController.view atIndex:0];

    [firstViewController viewDidDisappear:YES];
    [secondViewController viewDidAppear:YES];
}
[UIView commitAnimations];
    }

- (IBAction)switchViews:(id)sender {
if(self.secondViewController.view.superview == nil)
{
    [firstViewController.view removeFromSuperview];
    [self.view insertSubview:secondViewController.view atIndex:0];
}
    }

This does a nice animation but doesn't switch the view controllers.

Help please?

EDIT: Also putting another view inside the first view controller and how to switch the views will be helpful.

Lior Pollak
  • 3,362
  • 5
  • 27
  • 48

3 Answers3

3

In your code you're switching views and not view controllers in fact. This means that from the point of view of view controller internal messaging (e.g: view load/unload, view appear/disappear, autorotation), nothing changes, the original "current" view controller will still be at the top. Now in iOS this is normally managed using a container view controller, that is a view controller that "manages" a set of sub-viewcontrollers and takes care of internal messaging and transitions. The most common cases are the UINavigationController and the UITabBarController, with the iPad you can see also UISplitViewController and PageViewControllers.

With iOS5 Apple provided a way to inform the container view controller (which is an ordinary view controller) of changes of its contained VCs using the so-called "View Controller Containment". There is a video taken from WWDC 2011 and published on iTunes (for registered developers only) that you can see. Basically this methodology adds a few methods that allow to inform the container view controller of changes in its contained VCs structure. I will not enter in much detail here and invite to look at the WWDC video and read this stack overflow answer or this post. I think the latter is exactly what you're looking for.

Community
  • 1
  • 1
viggio24
  • 12,316
  • 5
  • 41
  • 34
  • The video is named "Implementing UIViewController Containment", session 102, 15 June 2011. You can access it through iTunes Store. – SmileBot Feb 20 '13 at 21:07
1

As the other poster says, you should not be swapping or mixing the content views of view controllers as you are doing. If you want to do that you need to use the new parent child view controller support in iOS 5.

If you DO use the new parent view controller support in iOS 5, there is a very cool, very powerful method called transitionFromViewController:toViewController:duration:options:animations:completion: that does most of the heavy lifting for you. It's pretty easy to use.

(I haven't watched the WWDC video on parent/child view controllers, but it would be a good place to start. I just started using the new methods.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You should use UINavigationController for switching between UIViewController.

[[self navigationController] pushViewController:firstViewController animated:YES];

Putting a UIView inside a UIViewController

[[self view] addSubview:view1];

And I would use UIView.hidden for switching the views

view1.hidden = YES;
view2.hidden = NO;
user866650
  • 19
  • 1
  • 4