SanitLee SanitLee - 1 month ago 13
Objective-C Question

Passing on tableview section title to another view's navigation bar title

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?

Answer
- (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];
}
Comments