Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在不同的ViewController中使用JSONModel数据?_Ios_Objective C_Xcode_Uiviewcontroller_Jsonmodel - Fatal编程技术网

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

Ios 如何在不同的ViewController中使用JSONModel数据?,ios,objective-c,xcode,uiviewcontroller,jsonmodel,Ios,Objective C,Xcode,Uiviewcontroller,Jsonmodel,我在objective c应用程序中使用JSONModel。我在第一个abBarController中将所有数据保存到我的JSONModel中。然后我需要在其他viewController中获取这些数据。我正在尝试将此数据发送到其他ViewController,如: 第一个视图控制器: 第二视图控制器: 有一种更好的形式可以保持此数据模型的实例化,并从另一个viewController获取模型数据,而无需将其发送到属性中?您可以使用singleton类,创建模型属性。 在另一个viewContr

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

第一个视图控制器:

第二视图控制器:


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

您可以使用singleton类,创建模型属性。
在另一个viewController中,可以通过singleton实例访问模型。参考

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

创建对象类

In.h对象类

In.m对象类

创建另一个对象类

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

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


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

在模型类中创建共享对象,在id对象中设置JSON对象,然后在任何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
@interface SecondViewController :UIViewController{
 MyModel *model;
}

@property MyModel *model;
@interface FirstModel : NSObject{
 }
@property(nonatomic,strong)NSMutableArray *productsArray;
-(id)init{
    self=[super init];
    if (self) {

        _productsArray=[[NSMutableArray alloc]init];

    }
    return self;
}
@interface SecondModel : NSObject
@property (nullable,nonatomic, retain) NSString *name;
@end
@property(nonatomic,strong)FirstModel *firstListObject;
SecondModel *prodObj=_firstListObject.productsArray[indexPath.item];
cell.productNameLabel.text=prodObj.name;