Search code examples
iphoneobjective-ciosuitableviewdidselectrowatindexpath

Programmatically edit title of controller in didSelectRowAtIndexPath


Previously, I was trying to go to another controller using the following code but failed

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RowDetail *rd = [[RowDetail alloc]initWithNibName:@"RowDetail" bundle:nil];
    if ([[allRow objectAtIndex:indexPath.row] is Equal:@"One"]) 
    {
        [rd setTitle:[allRow objectAtIndex:indexPath.row]];
    }
    [self.navigationalController pushViewController:rd animated:NO];
}

I am using a storyboard so I found the other code worked for me but I could not set the title for the controller I was pushing.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
    RowDetail *rd = [sb instantiateViewControllerWithIdentifier:@"RowDetail"];
    if ([[allRow objectAtIndex:indexPath.row] is Equal:@"One"]) 
    {
        [rd setTitle:[allRow objectAtIndex:indexPath.row]];
    }
    [self.navigationalController pushViewController:rd animated:NO];
}

It worked and showed the RowDetail controller but I could not edit the title. Any idea how can this be done?

EDIT: RowDetail.h

#import <UIKit/UIKit.h>

@interface LecturerDetail : UIViewController

@property (weak, nonatomic) IBOutlet UINavigationBar *rowNavBar;

@property (nonatomic, strong) NSString *lecDetailTitle;
@end

For the RowDetail, I tried using IBOutlet for the navigation bar but failed. I am still trying ways to let the title to show.


Solution

  • In RowDetailViewController

    @property (nonatomic, strong) NSString *rowDetailTitle;
    //synthesize in .m file too
    

    then

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
        RowDetail *rd = [sb instantiateViewControllerWithIdentifier:@"RowDetail"];
        rd.rowDetailTitle = @"yourTitle";
        [self.navigationalController pushViewController:rd animated:NO];
    }
    

    then in view did load of RowDetailViewController.m

    self.navigationItem.title = rowDetailTitle;
    

    hope it help, please give a feedback, thanks.

    UPDATE: or just simply do this:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
        RowDetail *rd = [sb instantiateViewControllerWithIdentifier:@"RowDetail"];
        rd.navigationItem.title = @"yourTitle";
        [self.navigationalController pushViewController:rd animated:NO];
    }