2017-02-09 4 views
0

私は目的のCアプリケーションでJSONModelを使用しています。最初のabBarControllerでJSONModelにすべてのデータを取得します。次に、他のviewControllerでこのデータを取得する必要があります。別のViewControllerで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; 

これを維持するためのより良いフォームがあります私のような他の人のviewControllersにこのデータを送信しようとしていますデータモデルはインスタンス化され、プロパティでこれを送信せずに別のviewControllerからモデルデータを取得しますか?

+0

は、あなたのモデルクラスで共有オブジェクトを作成し、IDのオブジェクトにJSONオブジェクトを設定...このオブジェクトクラスのアクセス権を持っていることがあり、その後の任意のViewControllerでアクセス。 –

+0

シングルトンを使用してモデルデータを設定または取得する –

答えて

0

シングルトンクラスを使用して、モデルプロパティを作成できます。 別のviewControllerでは、シングルトンのインスタンスを通してモデルにアクセスできます。 http://www.idev101.com/code/Objective-C/singletons.html

+0

アプリケーションのメモリにシングルトンを使用することは効率的ではありませんか? – user3745888

+0

メモリを気にしないでください。彼らは非常に小さいです – Dan

0

か、PLISTまたはデータ・ベース.Mで

1

オブジェクトクラスを作成します

.hのオブジェクトクラスで

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

を使用してローカルファイルにそれをアーカイブすることができますを参照オブジェクトクラス

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

     _productsArray=[[NSMutableArray alloc]init]; 

    } 
    return self; 
} 
TableviewViewcontroller .hファイルで インポート2つのオブジェクトクラスを別のオブジェクトクラス

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

を作成し、.Mファイル //セルでは、次のコード

@property(nonatomic,strong)FirstModel *firstListObject; 

を挿入

for rowAt Indexpath

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

あなたが必要な場所あなたが

関連する問題