Prepareforsegue navigationItem.title one behind

2019-07-31 07:20发布

问题:

I'm still relative new to object c, so please bear over with me if this is a rookie question. I'm trying to set the title of my navigationcontroller with the corresponding object information. I'm using prepareforsegue for it, but the first time is segue to the new controller, the title is blank. If i try again, it shows up, but if i pressed something else, it shows the title of the things i pressed the time before. I have embedded my code below.

//.h

#import <UIKit/UIKit.h>

@interface STATableViewController : UITableViewController

@property(strong,nonatomic)NSArray *listOfExercises;
@property(weak,nonatomic)NSString *navTitle;

@end

//.m

#import "STATableViewController.h"
#import "ExercisesViewController.h"

@implementation STATableViewController

@synthesize listOfExercises = _listOfExercises, navTitle = _navTitle;

- (void)viewDidLoad
{
    [super viewDidLoad];   
    _listOfExercises = [NSArray arrayWithObjects:@"Raketstart",@"SpeedBåd",@"Træstamme",nil]; 
    self.navigationItem.title = @"Exercises";    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_listOfExercises count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier];
    }

    NSString *cellValue = [_listOfExercises objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _navTitle = [_listOfExercises objectAtIndex:indexPath.row];
    //NSLog(_navTitle);
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"toExercise"])
    {
        ExercisesViewController *foo = [segue destinationViewController];
        foo.navigationItem.title= _navTitle;
    }
}

@end

回答1:

This is happening because prepareForSegue:sender: is being called before tableView didSelectRowAtIndexPath:. So you are always setting your navigationItem's title before you are setting your _navTitle property with the value you want.

Instead of getting the title in didSelectRowAtIndex path, do it in your prepareForSegue like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"toExercise"])
    {
        // "sender" is the table cell that was selected
        UITableViewCell *cell = (UITableViewCell*)sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        NSString *title= [_listOfExercises objectAtIndex:indexPath.row];

        ExercisesViewController *foo = [segue destinationViewController];
        foo.navigationItem.title = title;
    }
}