2017-02-09 37 views
0

我在我的目标c应用程序中使用JSONModel。我在第一个abBarController中获取所有数据给我的JSONModel。然后我需要在其他viewController中获取这些数据。我想这个数据发送给其他人viewControllers这样的:如何在不同的ViewControllers中使用JSONModel数据?

第一的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 

第二的ViewController:

@interface SecondViewController :UIViewController{ 
MyModel *model; 
} 

@property MyModel *model; 

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

+0

创建模型类的共享对象和ID对象设置JSON对象,然后访问它在任何视图控制器。 –

+0

使用单例设置或获取模型数据 –

回答

0

,或者您可以使用的plist或资料库

1

创建对象类

在.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文件 //细胞for rowAt Indexpath

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

您可以访问该对象类的你需要的地方......

相关问题