UITableView仅在第二次尝试时将数据发送到Detail View Controller

嘿,我一直在一个交通应用程序工作了一段时间,一直卡在这个问题一段时间了。

我正在使用iOS 5和故事板。 基本上我有一个UITableView显示最喜欢的公交车站位置,当用户select我使用的行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row]; stopString = favorite.title; routeString = favorite.subtitle; } 

通过用户select的单元格的停止和路线信息,然后准备与我的故事板上的一个segue相对应的segue,推送一个使用停靠点名称和路线名称的详细视图控制器来显示来自plist的时间。

我对Objective C和iOS相当陌生,所以我使用了一个我的朋友告诉我可以工作的segue,但是,这可能是问题所在。 segue看起来像这样:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UIViewController *destination = segue.destinationViewController; if ([destination respondsToSelector:@selector(setDelegate:)]) { [destination setValue:self forKey:@"delegate"]; } if ([destination respondsToSelector:@selector(setSelection:)]) { NSString *route = routeString; NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, @"route", stopString, @"stop", nil]; [destination setValue:selection1 forKey:@"selection"]; } } 

在我的DetailViewController中的Segue后,我在视图DidLoad中获取停止和路由信息:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. route = [selection objectForKey:@"route"]; stopName = [selection objectForKey:@"stop"]; NSLog(@"stopName: %@", stopName); NSLog(@"routeName: %@", route); } 

这是我的问题出现的地方。 当我运行模拟器并在我的表格视图中单击一个单元格时,我被推送到DVC,但是,stopName和routeName都是空的,所以没有信息被发送或接收。 但是,如果我回到表中并单击另一个单元格,routeName和stopName填充了我第一次单击单元格时发送的信息。 如果我继续这个过程,它将继续发送先前未被触发的单元的信息。

所以基本上信息是发送,但只有我经过两次segue。 很显然,我希望它能够第一时间发送信息并接收信息,但是这个信息被拖延了,并且让我很吃惊。 我感谢任何帮助的人可以给我,因为我一直在寻找互联网的日子,现在试图解决这个问题,非常感谢你提前任何帮助!

prepareForSegue:didSelectRowAtIndexPath:之前被调用。 这就是为什么你看到的价值总是落后的原因。

更好的解决scheme是在您的prepareForSegue:方法中获取stopString和routeString值(而不是使用didSelectRowForIndexPath:根本)。 这样做的关键是要认识到,传递给prepareForSegue:sender参数值是被轻敲的UITableViewCell。 您可以使用UITableView方法indexPathForCell在表中获取单元格的indexPath,然后用它来查找您的favoriteItems数组中的数据。

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UITableViewCell *cell = (UITableViewCell*)sender; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row]; stopString = favorite.title; routeString = favorite.subtitle; UIViewController *destination = segue.destinationViewController; if ([destination respondsToSelector:@selector(setDelegate:)]) { [destination setValue:self forKey:@"delegate"]; } if ([destination respondsToSelector:@selector(setSelection:)]) { NSString *route = routeString; NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, @"route", stopString, @"stop", nil]; [destination setValue:selection1 forKey:@"selection"]; } } 

确保你没有将segue直接连接到tableView CELL的下一个视图控制器。 连接到整个UITableViewController / UIViewController(无论你正在使用),并给一个名字,说“segueNameInStoryboard”。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row]; stopString = favorite.title; routeString = favorite.subtitle; /* add this line */ [self performSegueWithIdentifier:@"segueNameInStoryboard" sender:self]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"segueNameInStoryboard"]) { UIViewController *nextViewController = segue.destinationViewController; nextViewController.delegate = self; NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:routeString, @"route", stopString, @"stop", nil]; nextViewController.selection = selection1; } }