我通过点击当前 Controller 的单元格来初始化一个新的 View Controller 。我只想通过引用将单元格中的对象传递给下一个 Controller ,这样如果我更改新 Controller 中的对象,它应该在原始位置更改。
我已经尝试了很多关于它的答案,但一无所获。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
RecentItemDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RecentItemDetailViewController"];
vc.station = self.stations[indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
我想通过引用在 vc.station
中获取 self.stations[indexPath.row]
对象,所以如果我更改 vc.station
然后 self.stations[indexPath.row]
也应该改变。
self.stations
和vc.station
都是NSMutableArray
类型。
最佳答案
你已经做对了。您最有可能缺少的是在您返回时重新加载 TableView 。
你可以重写view will appear方法。然后检查 TableView 中选定的行是什么。重新加载该行。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(self.tableView.indexPathForSelectedRow) {
[self.tableView reloadRowsAtIndexPaths:@[tableView.indexPathForSelectedRow] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// Or just reload the whole thing
[self.tableView reloadData];
}
但这自然假设您永远不会分配给 RecentItemDetailViewController
内的 station
属性。您可以调用 [self.station add...]
但永远不要调用 self.station = ...
。
关于ios - 如何通过引用将对象从一个类传递到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129198/