0

I have an iPad application that has a button on one view. When I press the button I want it to load a second view. The second view I am trying to load is a CollectionView. I am not using, and do not want to use a UINavigationController.

Does anyone know how to load a second view on a button tap? Also, I will want to create a Back button that will go back to the previous view. The previous view could be different each time the button is tapped.

There is a decent amount of material online about this topic, but I can't find anything that will work or anything that is recent.

Here is the code I have now:

-(void)showCollectionView:(id)sender
{
    NSLog(@"In ShowCollectionView");

    ZHCollectionViewController *cvc = [[ZHCollectionViewController alloc]
                              initWithNibName:@"ZHCollectionViewController"
                              bundle:[NSBundle mainBundle]];
    [self.view addSubview:cvc.view];

    NSLog(@"After all the stuff");
}

When this runs both NSLog's are executed and the message shows up in the console, but nothing happens to the view.

tentmaking
  • 2,076
  • 4
  • 30
  • 53
  • This should work fine. I guess there is a problem with `ZHCollectionViewController` somewhere. Maybe the way its view is created. – Petar Mar 26 '13 at 16:43

2 Answers2

0

Yo can try to present it modally:

[self presentViewController:cvc animated:YES completion:^{
                                                         }];

Before this call you can customize appearance of your 'cvc' by defining transition and presentation styles, for example:

cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.modalPresentationStyle = UIModalPresentationFormSheet;

To hide it call in ZHCollectionViewController, inside some button action I believe

[self dismissViewControllerAnimated:NO completion:^{  
                                                   }];
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • Did you mean to say presentedViewController or presentingViewController, presentViewController is not available. – tentmaking Mar 26 '13 at 17:05
  • Can you explain the [self presentViewController...] more. I can't get that line to work. The animated argument is not available, I get a "No Visible @Interface" error when I try to implement that line – tentmaking Mar 26 '13 at 17:07
  • @ZacharyHunt, self mean a UIViewController subclass (in my example). If you can't find it, then most likely you are on the UIView and you need to find your current view controller. – beryllium Mar 26 '13 at 17:57
0

There are several ways to do this, and the way you're trying isn't one of them. If you just want to add a view, not a view controller, you should have a xib file that is a view, not a view controller. You would have to make the controller whose view you're adding this collection view into, the files owner of this collection view, so you can hook up any outlets to it.

It's not correct to add another view controller's view to your view, unless you're making that controller a child controller. If you want ZHCollectionViewController to be the controller of the collection view, then you should add that controller as a child view controller. You can check out Apple's documentation on custom container controllers to see how that's done.

You didn't really say in your question, how this collection view is to appear. Do you want it to take up the whole screen, or do you want it to be a subview? If you want it to take up the whole screen, then it would be better to just change the window's root view controller to ZHCollectionViewController, or present it modally over the current view.

rdelmar
  • 103,982
  • 12
  • 207
  • 218