如何在不同的ViewControllers中使用JSONModel数据?

时间:2022-01-29 14:23:29

I'm using JSONModel in my objective c application. I get all data to my JSONModel in a first abBarController. Then I need get this data in other viewController. I'm trying send this data to the others viewControllers like:

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

First ViewController:

First 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

Second ViewController:

第二个ViewController:

@interface SecondViewController :UIViewController{
 MyModel *model;
}

@property MyModel *model;

There is a better form to keep this data model instantiated and get the model data from another viewController without send this in a property?

有一个更好的形式来保持这个数据模型的实例化,并从另一个viewController获取模型数据而不在属性中发送它?

3 个解决方案

#1


1  

Create object class

创建对象类

In .h object class

在.h对象类中

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

In .m object Class

在.m对象类中

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

        _productsArray=[[NSMutableArray alloc]init];

    }
    return self;
}

Create another one Object Class

创建另一个Object Class

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

In TableviewViewcontroller .h file Import Two Object class and insert the following coding

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

@property(nonatomic,strong)FirstModel *firstListObject;

In .m file //cell for rowAt Indexpath

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

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

You may have access this object class wherever you need…

您可以在任何需要的地方访问此对象类...

#2


0  

You can use a singleton class, create your model property. In another viewController, you can access your model through instance of singleton. Reference http://www.idev101.com/code/Objective-C/singletons.html

您可以使用单例类,创建模型属性。在另一个viewController中,您可以通过singleton实例访问您的模型。参考http://www.idev101.com/code/Objective-C/singletons.html

#3


0  

or you can archive it to the local files using plist or data-base

或者您可以使用plist或数据库将其存档到本地文件

#1


1  

Create object class

创建对象类

In .h object class

在.h对象类中

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

In .m object Class

在.m对象类中

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

        _productsArray=[[NSMutableArray alloc]init];

    }
    return self;
}

Create another one Object Class

创建另一个Object Class

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

In TableviewViewcontroller .h file Import Two Object class and insert the following coding

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

@property(nonatomic,strong)FirstModel *firstListObject;

In .m file //cell for rowAt Indexpath

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

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

You may have access this object class wherever you need…

您可以在任何需要的地方访问此对象类...

#2


0  

You can use a singleton class, create your model property. In another viewController, you can access your model through instance of singleton. Reference http://www.idev101.com/code/Objective-C/singletons.html

您可以使用单例类,创建模型属性。在另一个viewController中,您可以通过singleton实例访问您的模型。参考http://www.idev101.com/code/Objective-C/singletons.html

#3


0  

or you can archive it to the local files using plist or data-base

或者您可以使用plist或数据库将其存档到本地文件