0

I'm attempting to create a subview inside a regular view. When a button is clicked that is contained in this subview, it will flip over and show a new subview. This will is meant to simulate a card flipping over on a table. I have a DetailViewController that is an empty xib, but when it's loaded it loads another controller called CardFrontViewController that actually contains the subview (front of the card) all contained in a xib. Once the button inside the subview, it should load the subview that is contained inside the CardBackViewController xib.

Inside DetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (!self.cardFrontViewController)
    {
        self.cardFrontViewController = [[CardFrontViewController alloc] initWithNibName: @"CardFrontViewController" bundle: nil];
        [self.view addSubview: self.cardFrontViewController.view];
    }

    [self configureView];
}

Inside CardFrontViewController

- (IBAction)flipToAnswerCard:(id)sender 
{
    if (!self.cardBackViewController)
    {
        self.cardBackViewController = [[CardBackViewController alloc] initWithNibName: @"CardBackViewController" bundle: nil];
    }

    [UIView transitionWithView: self.cardFrontContainerView
                      duration: 1.0
                       options: UIViewAnimationOptionTransitionFlipFromTop 
                    animations: ^{ [self.cardFrontSubView removeFromSuperview];
                        [self.cardFrontContainerView addSubview: self.cardBackViewController.cardBackSubView]; }
                    completion: nil];
}

When I click the button to flip over the card, the card flips, but the view that is displayed is empty, meaning that the self.cardBackViewController.cardBackSubView is not loaded inside. If I do [self.cardFrontContainerView addSubview: self.cardBackViewController.view] inside the flip card method it loads the entire xib inside, but I really just want a view inside. I'm not sure what to do here.

5StringRyan
  • 3,604
  • 5
  • 46
  • 69

2 Answers2

2

You don't need a UIViewController if all you want is just to load a view, with some IBOutlets.

Just use [[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil] from your DetailViewController, and if IBOutlets corresponding to ne 'card' NIB exist, they will automatically be linked (to the owner).

If you really want a different class to handle this 'card' (and 'own' the IBOutlets) does it necessarily need to inherit from UIViewController? NSObject can sometimes be a good candidate too.

Finally, If you really really want to nest UIViewControllers, this will only work in iOS5. To support previous iOS versions, you would eventually have to manually call some UIViewController's life-cycle methods (willAppear, didAppear...), pass certain events from you master UIViewController to its detailViewController (like willAnimateToRotation), and maybe some other tricks.

Vinzzz
  • 11,746
  • 5
  • 36
  • 42
  • I feel like I'm trying to do something that is relatively simple, just a view that can flip to another view inside a view using two xibs, why is it so complicated and why is it hard to find resources online? Based on your answer, it doesn't seem like what I'm trying to do is the way to do it. – 5StringRyan Nov 27 '11 at 05:33
  • Then just use UIView ! and you can call the method above (`loadNibNamed:`) to load a specific View class, with it's own outlets – Vinzzz Nov 27 '11 at 23:53
1

If you just want to flip subviews within a main view, the answer that worked for me was found here: https://stackoverflow.com/a/9028156/1319615

Community
  • 1
  • 1
Han
  • 173
  • 1
  • 9