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!!
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.
Comments