Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Prepareforsegue navigationItem.title一后面_Objective C_Xcode_Uitableview_Uinavigationitem - Fatal编程技术网

Objective c Prepareforsegue navigationItem.title一后面

Objective c Prepareforsegue navigationItem.title一后面,objective-c,xcode,uitableview,uinavigationitem,Objective C,Xcode,Uitableview,Uinavigationitem,我对对象c还是比较陌生的,所以如果这是一个新手问题,请耐心听我说。我正在尝试使用相应的对象信息设置navigationcontroller的标题。我使用的是prepareforsegue,但第一次使用的是新控制器,标题为空。如果我再试一次,它会显示出来,但是如果我按下其他按钮,它会显示我上次按下的按钮的标题。我在下面嵌入了我的代码 //.h #import <UIKit/UIKit.h> @interface STATableViewController : UITableVie

我对对象c还是比较陌生的,所以如果这是一个新手问题,请耐心听我说。我正在尝试使用相应的对象信息设置navigationcontroller的标题。我使用的是prepareforsegue,但第一次使用的是新控制器,标题为空。如果我再试一次,它会显示出来,但是如果我按下其他按钮,它会显示我上次按下的按钮的标题。我在下面嵌入了我的代码

//.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
发生这种情况是因为在tableView DidSelectRowatineXpath:之前调用了prepareForSegue:sender:。因此,在使用所需的值设置_navTitle属性之前,您总是先设置navigationItem的标题

不要在didSelectRowAtIndex路径中获取标题,而是在您的PrepareForegue中执行以下操作:

-(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;
    }
}
发生这种情况是因为在tableView DidSelectRowatineXpath:之前调用了prepareForSegue:sender:。因此,在使用所需的值设置_navTitle属性之前,您总是先设置navigationItem的标题

不要在didSelectRowAtIndex路径中获取标题,而是在您的PrepareForegue中执行以下操作:

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