将viewController的标题更改为tabbarController

时间:2014-03-11 21:17:07

标签: ios objective-c ios6 uitabbarcontroller uicollectionview

我的应用程序第一个视图是UICollectionView,我将Cell推送到TabBarController有五个视图,但是当我更改每个视图的标题时没有出现问题。所以我需要为这五个viewController添加不同的标题。

CollectionView.m是:

- (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);
}

2 个答案:

答案 0 :(得分:1)

您需要创建tabBarController。你的didSelect方法中有类似的东西。 ...

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];

另一方面,在导航控制器内部放置一个标签栏被认为是不好的形式(至少是Apple),我认为甚至不可能在导航栏中放置一个导航栏。

进一步的想法(以下评论后):

我建议的是从原始collectionView“呈现”你的tabBarController,然后标签栏中的每个viewControllers都是一个navController。在你的didSelect中,现在看起来像:

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];

在Interface Builder中为VC创建视图(比如MYView1)(编辑>新>文件> ios> UserInterface>视图>并将File的OwnerType更改为VC类的名称)。然后,而不只是[[MYCustomViewController1 alloc] init]使用[[MYCustomViewController1 alloc] initWithNibName:@"MYView1" bundle:nil]

如果要从tabBar返回到collectionView,从任何嵌入式控制器,请创建一个按钮并链接其操作以运行此代码:

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

最后,如果你想在IB中完成所有这些操作,只需使用相关的五个子控制器绘制tabcontroller。然后让你的collectionViewController(不应该在导航控制器中),并从集合ViewCell创建一个segue到tabController。将segue的类型设置为modal。然后单击一个项目应该直接启动tabController。要设置tabController的productNumber信息,请添加以下方法:

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

答案 1 :(得分:0)

创建视图控制器时,需要为UITabBarItem属性分配tabBarItem。如果您只想要一个标题,可以只分配给title属性。

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

UICollectionViewController *secondVC = ...
secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Title2" image:[UIImage imageNamed:@"image"] tag:0];