0

I have a main view (view1) with this below code. When I tap a cell in a section, it will push to another view (view2) and I want to have that view2's title to be the same as view1's section title that I tap on.

view1

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:headerView.frame];
    imageView.image = [UIImage imageNamed:@"bg_blue.png"];
    [headerView addSubview:imageView];

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, kScreenWidth, 20)];
    title.textColor = [UIColor whiteColor];
    CategoryListData *data = self.dataArray[section];
    title.text = data.categoryTitle;
    [headerView addSubview:title];

    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MainHeaderTableViewCell *tableCell = (MainHeaderTableViewCell *)collectionView.superview.superview;
    CategoryListData *data = self.dataArray[tableCell.index];
    CategoryDetailData *detailData = data.categoryList[indexPath.row];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    View2 *vc = [storyboard instantiateViewControllerWithIdentifier:@"View2"];
    vc.detailData = detailData;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

view2

- (void)initNavigationTitle {
    [self setupNavigationTitle:self.detailData.title backButtonWithAnimated:YES]; // this line need to be replaced with section name from view1

}

I'm trying to do something like this in view2

CategoryListData *data = self.dataArray[section];
title.text = data.categoryTitle;
[self setupNavigationTitle:title.text backButtonWithAnimated:YES];

But how can I pass on parameter section from view1 to view2?

SanitLee
  • 1,253
  • 1
  • 12
  • 29

1 Answers1

1
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MainHeaderTableViewCell *tableCell = (MainHeaderTableViewCell *)collectionView.superview.superview;
    CategoryListData *data = self.dataArray[tableCell.index];
    CategoryDetailData *detailData = data.categoryList[indexPath.row];

    NSString *sectionTitle = [self.dataArray[tableCell.index] categoryTitle];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    View2 *vc = [storyboard instantiateViewControllerWithIdentifier:@"View2"];
    vc.detailData = detailData;
    vc.title = sectionTitle;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
SanitLee
  • 1,253
  • 1
  • 12
  • 29
Sachin Vas
  • 1,757
  • 12
  • 16
  • Thanks but there's a problem when I tap another section then go to view2 it still shows title from my first tap. How can I refresh that? – SanitLee Oct 14 '16 at 03:56
  • 1
    It shouldn't happen. Because you are creating new viewController every time you are selecting the cell. Debug to see if the sectionTitle String value is proper. – Sachin Vas Oct 14 '16 at 03:58
  • got it! i changed to this `NSString *sectionTitle = [self.dataArray[tableCell.index] categoryTitle];` – SanitLee Oct 14 '16 at 04:21