Question

My App first view is UICollectionView and I push the Cell to TabBarController with five views but the problem when I change the title for each view didn't appear. So I need to put a different titles for these five viewController.

CollectionView.m is:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    productName *data = [self.productarray objectAtIndex:indexPath.item];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    imageView.image = data.productImage;

    UILabel *title2 = (UILabel *)[cell viewWithTag:200];
    title2.text = data.productNames;

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ProductsViewController *detail = [[ProductsViewController alloc]init];
    //[self.view pushViewController:detail animated:YES];
    detail.productNumber = indexPath.item;
    //NSLog(@"%d", indexPath.item);
}
Was it helpful?

Solution

You need to create the tabBarController. Something like this in your didSelect method. ...

UITabBarController * tabBarController = [[UITabBarController alloc] init];

UIViewController * firstController = [[MYCustomViewController alloc] init];
UITabBarItem * firstItem = [[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed: @"FirstImage"] tag:1];
firstController.tabBarItem = firstItem;

//Now create second through fifth VCs here

tabBarController.viewControllers =  @[firstController, secondController, thirdController, fourthController, fifthController];
[self.navigationController pushViewController:tabBarController animated:YES];

On the other hand, it's considered bad form (by Apple at least) to put a tab bar inside a navcontroller, and I don't think it's even possible to put a nav bar inside a nav bar.

Further thoughts (After comment below):

What I would suggest instead would be to "present" your tabBarController from your original collectionView, then each of the viewControllers inside the tab bar would be a navController. In your didSelect, this would now look like:

UITabBarController * tabBarController = [[UITabBarController alloc] init];

UINavigationController *firstController = [[UINavigationController alloc] initWithRootViewController:[[MYCustomViewController1 alloc] init];
UITabBarItem * firstItem = [[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed: @"FirstImage"] tag:1];
firstController.tabBarItem = firstItem;

//Now create second through fifth Nav VCs here

tabBarController.viewControllers =  @[firstController, secondController, thirdController, fourthController, fifthController];

tabBarController.modalPresentationStyle = UIModalPresentationFullScreen;
tabBarController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentViewController:tabBarController animated:YES completion:nil];

To create a view (say MYView1) for a VC in Interface Builder (Edit>New>File>ios>UserInterface>View> and change File's OwnerType to the name of your VC class). Then instead of just [[MYCustomViewController1 alloc] init] use [[MYCustomViewController1 alloc] initWithNibName:@"MYView1" bundle:nil]

When you want to return from the tabBar back to the collectionView, from any of the embedded controllers, create a button and link its action to run this code:

[self.tabBarController dismissViewController:self.tabBarController animated:YES completion:nil]

Finally, if you want to do ALL this in IB, just draw your tabcontroller with the associated five subcontrollers. Then have your collectionViewController (which should NOT be within a nav controller), and create a segue from your collectionViewCell to the tabController. Set the type of the segue to modal. Then clicking an item should launch the tabController directly. To set the productNumber information for the tabController, add this method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender   {
    NSIndexPath *indexPath = [self.collectionView indexPathForSelectedRow];
   segue.destinationViewController.productNumber = indexPath.item;
}

OTHER TIPS

You need to assign a UITabBarItem to the tabBarItem property when you create your view controllers. If you just want a title you can just assign to the title property.

UITableViewController *firstVC = ...
firstVC.title = @"Title";

UICollectionViewController *secondVC = ...
secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Title2" image:[UIImage imageNamed:@"image"] tag:0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top