【问题标题】:change the title for viewController into tabbarController将 viewController 的标题更改为 tabbarController
【发布时间】:2014-04-15 17:15:29
【问题描述】:

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

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

【问题讨论】:

  • 你能把问题说清楚点吗?您的实际问题是什么 - 单元格或视图控制器未显示详细信息?您还继承UICollectionViewCell 并创建自定义imageView 和标签?
  • 我的问题是我的第一个 viewController 是 uicollectionViewController,我用五个不同的 viewController 装到 tabbarViewController 但我需要为每个 viewController 放置不同的标题和 itemBarButton

标签: ios objective-c ios6 uitabbarcontroller uicollectionview


【解决方案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];

另一方面,将标签栏放在导航控制器中被认为是不好的形式(至少苹果公司),我认为甚至不可能将导航栏放在导航栏中。

进一步的想法(在下面的评论之后):

我的建议是从您的原始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>用户界面>视图>并将文件的 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(它不应该在导航控制器中),并从你的collectionViewCell 到tabController 创建一个segue。将 segue 的类型设置为模态。然后单击一个项目应该直接启动 tabController。为 tabController 设置 productNumber 信息,添加这个方法:

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

【讨论】:

  • 把这段代码放在didSelect方法上或者创建uitabbarcontroller类;请指教
  • 不管怎样。在 didSelect 中,或者在子类 UITBC 中的 viewDidLoad 中(第一行和最后一行除外)
  • 我需要告诉你,我有两个导航栏,一个用于 uitabbarcontroller,另一个用于每个视图控制器我每个都有一个 navigationcontroller,所以我认为 tabbarcontroller 导航栏显示的标题
  • 然后看上面的修订注释。
  • 你能告诉我我把[self.tabBarController dismissViewController:self.tabBarController animated:YES completion:nil] 放在每个viewDidload 的什么地方吗?其次,我使用情节提要和代码工作,但情节提要上的 allViews 没有显示;你能告诉我我能做些什么吗
【解决方案2】:

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

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

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

【讨论】:

  • 我的问题是我的第一个 viewController 是 uicollectionViewController,我用五个不同的 viewController 装到 tabbarViewController 但我需要为每个 viewController 放置不同的标题和 itemBarButton
  • 5 个不同标签的信息从何而来?
  • firstViewController是uitableViewController,secondViewController是uiCollectionViewController,剩下的是UIViewController。所以我需要为每个和 itemBarButton 放置不同的标题
  • 抱歉打扰了
  • 不,没关系,你的问题中有项目选择代码,从最初的措辞来看,当你点击一个单元格时,听起来你想更改标题:)
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
相关资源
最近更新 更多