Initializing and switching between two UICollectionViews

tbogatchev

So I have two UICollectionViews that have different types of layouts, different types of cells, and different data in the cells, hence I have decided to make two separate views rather than dealing with keeping track of all of their data.

I made almost all the UI in the Interface editor, so I have .xib files for both my UICollectionViews (lets call them NewsCollectionView and ExploreCollectionView) and all of my cells. The two .xib files are connected to their controllers through the FilesOwner property in the Interface Builder.

I also have a main view (lets call it HomeViewController) with a tabBar which I want to use to switch between my two UICollectionViews. My first question is simple, how do I initialize one of my UICollectionViews from the NIB?

Here is how I imagined it (in my HomeViewController)

@property (strong, nonatomic) NewsCollectionView *newsCollectionView;
@property (strong, nonatomic) ExploreCollectionView *exploreCollectionView;

- (void)viewDidLoad {

[super viewDidLoad];
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"NewsCollectionView" owner:self options:nil];
self.newsCollectionView = [arrayOfViews objectAtIndex:0];


[self.view addSubview:self.newsCollectionView];

The result of that code is a message stating "Can't add self as subview".

My second question is about switching between the two collectionViews. I imagine it will go something like this:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{


if(item == newsEventsTab)
{
    [UIView transitionFromView:self.exploreCollectionView
                     toView:self.newsCollectionView
                   duration:0.25
                    options:UIViewAnimationTransitionFlipFromRight
                 completion:nil];
}
if(item == exploreTab)
{
    [UIView transitionFromView:self.newsCollectionView
                     toView:self.exploreCollectionView
                   duration:0.25
                    options:UIViewAnimationTransitionFlipFromRight
                 completion:nil];
}    
}

However I have no idea if that would work because I can't even get the views to show up yet. Please help me out, thanks!!

tbogatchev

I ended up accomplishing my task by creating a containerView inside my HomeViewController, and connecting it via IBOutlet.

In order to set up my collection views I did this:

self.newsCollectionView = [[NewsCollectionView alloc] initWithNibName:@"NewsCollectionView" bundle:nil];
self.newsCollectionView.view.frame = self.collectionContainer.bounds;
self.exploreCollectionView = [[ExploreCollectionView alloc] initWithNibName:@"ExploreCollectionView" bundle:nil];
self.exploreCollectionView.view.frame = self.collectionContainer.bounds;

[self addChildViewController:self.newsCollectionView];
[self addChildViewController:self.exploreCollectionView];

[self.collectionContainer addSubview:self.newsCollectionView.view];

[self.newsCollectionView didMoveToParentViewController:self];
[self.exploreCollectionView didMoveToParentViewController:self];

The view transition is as I described in my original post, except you have to reference the actual view instead of the controller (so self.newsCollectionView.view instead of just self.newsCollectionView).

Works like a charm.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Switching between two storyboards

From Dev

Switching between two images

From Dev

Switching between two storyboards

From Dev

Switching Between Two Slideshows with Javascript

From Dev

What is the difference between this two ways of initializing String

From Dev

What is the difference between these two ways of initializing a HashMap?

From Dev

What is the difference between this two ways of initializing String

From Dev

Two UICollectionViews and two UICollectionViewCells in one UIViewController

From Dev

Switching between two .virmc settings with a command?

From Dev

Switching between two strings like booleans

From Dev

How to handle switching between two databases

From Dev

Switching between two div elements using Javascript

From Dev

NullPointerException when switching between two Activities on Android

From Dev

Switching between two div elements using Javascript

From Dev

Automatically switching between two running programs

From Dev

Switching between two forms with Key Bindings

From Dev

Switching between two idle animation states

From Dev

switching my imageView between two sources

From Dev

UISegmentedControl with two UICollectionViews to display images. MULTITHREADING

From Dev

Can two UICollectionViews respond to a single gesture?

From Dev

UISegmentedControl with two UICollectionViews to display images. MULTITHREADING

From Dev

Adding two UICollectionViews in a ViewController with stores programmatically

From Dev

What is the difference between initializing hash two times and clear?

From Dev

Switching between two select drop down in angular js

From Dev

keyboard short cut for switching between two or more instances of the same application?

From Dev

Can I use one definition of two Button by switching between them?

From Dev

ASP: Switching between two ScriptManagers. Or how to remove script reference

From Dev

keyboard short cut for switching between two or more instances of the same application?

From Dev

Switching between two select drop down in angular js

Related Related

  1. 1

    Switching between two storyboards

  2. 2

    Switching between two images

  3. 3

    Switching between two storyboards

  4. 4

    Switching Between Two Slideshows with Javascript

  5. 5

    What is the difference between this two ways of initializing String

  6. 6

    What is the difference between these two ways of initializing a HashMap?

  7. 7

    What is the difference between this two ways of initializing String

  8. 8

    Two UICollectionViews and two UICollectionViewCells in one UIViewController

  9. 9

    Switching between two .virmc settings with a command?

  10. 10

    Switching between two strings like booleans

  11. 11

    How to handle switching between two databases

  12. 12

    Switching between two div elements using Javascript

  13. 13

    NullPointerException when switching between two Activities on Android

  14. 14

    Switching between two div elements using Javascript

  15. 15

    Automatically switching between two running programs

  16. 16

    Switching between two forms with Key Bindings

  17. 17

    Switching between two idle animation states

  18. 18

    switching my imageView between two sources

  19. 19

    UISegmentedControl with two UICollectionViews to display images. MULTITHREADING

  20. 20

    Can two UICollectionViews respond to a single gesture?

  21. 21

    UISegmentedControl with two UICollectionViews to display images. MULTITHREADING

  22. 22

    Adding two UICollectionViews in a ViewController with stores programmatically

  23. 23

    What is the difference between initializing hash two times and clear?

  24. 24

    Switching between two select drop down in angular js

  25. 25

    keyboard short cut for switching between two or more instances of the same application?

  26. 26

    Can I use one definition of two Button by switching between them?

  27. 27

    ASP: Switching between two ScriptManagers. Or how to remove script reference

  28. 28

    keyboard short cut for switching between two or more instances of the same application?

  29. 29

    Switching between two select drop down in angular js

HotTag

Archive