ios - 如何在不同的 ViewController 中使用 JSONModel 数据?

标签 ios objective-c xcode uiviewcontroller jsonmodel

我在 Objective C 应用程序中使用 JSONModel。我将所有数据获取到第一个 abBarController 中的 JSONModel。然后我需要在其他 viewController 中获取这些数据。我正在尝试将此数据发送给其他 viewControllers,例如:

第一个 ViewController:

@implementation FirstViewController 
...
SecondViewController* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
SecondViewController.model = self.model;//Here send the model with data
[self.navigationController pushViewController:infoController animated:YES];
...
@end

第二个 View Controller :

@interface SecondViewController :UIViewController{
 MyModel *model;
}

@property MyModel *model;

是否有更好的形式来保持此数据模型实例化并从另一个 viewController 获取模型数据,而无需将其发送到属性中?

最佳答案

创建对象类

在.h对象类中

@interface FirstModel : NSObject{
 }
@property(nonatomic,strong)NSMutableArray *productsArray;

在.m对象类中

-(id)init{
    self=[super init];
    if (self) {

        _productsArray=[[NSMutableArray alloc]init];

    }
    return self;
}

创建另一个对象类

@interface SecondModel : NSObject
@property (nullable,nonatomic, retain) NSString *name;
@end

在 TableviewViewcontroller.h 文件中 导入两个对象类并插入以下编码

@property(nonatomic,strong)FirstModel *firstListObject;

在 .m 文件中 //rowAt Indexpath 的单元格

SecondModel *prodObj=_firstListObject.productsArray[indexPath.item];
cell.productNameLabel.text=prodObj.name;

你可以在任何需要的地方访问这个对象类......

关于ios - 如何在不同的 ViewController 中使用 JSONModel 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132468/

相关文章:

ios - 在 iOS 应用程序中使用 HeroIcons 时模糊的 SVG 渲染

objective-c - 如何从模拟器中的核心位置获得所需的精度?

ios - 未调用 CXProviderDelegate 方法

iphone - Testflight错误: This build doesn't support iPhone 5c GSM.请求开发者支持您的设备

cocoa - 有关未在文件内容中进行替换的 Xcode 项目模板的特定帮助

ios - iPhone X 上的 Ionic 插件 ThemeableBrowser 偏移问题

iOS 接入点 : Can two notification provider servers have same device token

ios - 共享时 UIActivityViewController 不显示 facebook 上的链接

objective-c - 是否可以在静态库中使用 FlurryAPI?

ios - 为同一个项目编译不同的代码 - Swift 框架