Приготовьтесь к поиску навигации - PullRequest
1 голос
/ 16 марта 2012

Я все еще относительно новичок в объекте c, так что, пожалуйста, потерпите меня, если это вопрос новичка.Я пытаюсь установить заголовок моего навигационного контроллера с соответствующей информацией об объекте.Я использую подготовить запрос для этого, но первый раз переход на новый контроллер, название пустое.Если я попробую еще раз, он появится, но если я нажал что-то еще, он покажет название вещей, которые я нажал в прошлый раз.Я ввел свой код ниже.

//.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 Ответ

5 голосов
/ 16 марта 2012

Это происходит потому, что prepareForSegue:sender: вызывается раньше tableView didSelectRowAtIndexPath:.Таким образом, вы всегда устанавливаете заголовок вашего navigationItem перед тем, как задать свойству _navTitle требуемое значение.

Вместо того, чтобы получать заголовок в пути didSelectRowAtIndex, сделайте это в вашем prepareForSegue следующим образом:

-(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;
    }
}
...