Prepareforsegue navigationItem.title 一个落后 [英] Prepareforsegue navigationItem.title one behind

查看:23
本文介绍了Prepareforsegue navigationItem.title 一个落后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对对象 c 还比较陌生,所以如果这是一个菜鸟问题,请耐心等待.我正在尝试使用相应的对象信息设置导航控制器的标题.我正在为此使用 prepareforsegue,但第一次是转至新控制器,标题为空白.如果我再试一次,它会出现,但如果我按下其他东西,它会显示我之前按下的东西的标题.我在下面嵌入了我的代码.

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

推荐答案

发生这种情况是因为 prepareForSegue:sender:tableView didSelectRowAtIndexPath: 之前被调用.因此,在将 _navTitle 属性设置为所需值之前,您总是先设置导航项的标题.

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.

不要在 didSelectRowAtIndex 路径中获取标题,而是在你的 prepareForSegue 中这样做:

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

这篇关于Prepareforsegue navigationItem.title 一个落后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆