我在 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/