ios - 如何通过引用将对象从一个类传递到另一个类?

标签 ios objective-c iphone

我通过点击当前 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.stationsvc.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/

相关文章:

ios - 台风从 Storyboard 中将属性注入(inject) View Controller

objective-c - UIWebView 尖角

ios - Swift SpriteKit '($T15) -> $T16' 与 'SKEmitterNode' 不同

ios - “名称”不可用 : not available on iOS - XCode11

ios - 对包含 NSRange 值的数组进行排序

c# - 带有(一点点)Unity 3D 的 iOS 项目?

iphone同时调用if和else?

iphone - 查找最接近我当前 GPS 位置的坐标

ios - 在具有 VIPER 架构的应用程序上更新 NSManagedObject

ios - kotlin.UInt 无法转换为 kotlinx.cinterop.CValuesRef