How to use JSONModel data in different ViewControllers? - ios

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:
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:
#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?

Create object class
In .h object class
#interface FirstModel : NSObject{
}
#property(nonatomic,strong)NSMutableArray *productsArray;
In .m object Class
-(id)init{
self=[super init];
if (self) {
_productsArray=[[NSMutableArray alloc]init];
}
return self;
}
Create another one 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
#property(nonatomic,strong)FirstModel *firstListObject;
In .m file
//cell for rowAt Indexpath
SecondModel *prodObj=_firstListObject.productsArray[indexPath.item];
cell.productNameLabel.text=prodObj.name;
You may have access this object class wherever you need…

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

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

Related

How to Push Data From one WKInterfaceController to Another?

I have 2 Interface Controllers in my WatchKit. The first one is called InterfaceController while the other is called DetailsForWatch. IC has a tableView on it. It parses data from a Parse class, and displays data from each entry in the class as a row. This works fine.
What I am trying to do is pass the PFObject for the selected row to a PFObject in DetailsForWatch. My setup for DFW is:
.h
#interface DetailsForWatch : WKInterfaceController {
}
#property (nonatomic, retain) IBOutlet WKInterfaceLabel *detailsLabel;
#property (nonatomic, retain) PFObject *finalObject;
#end
.m
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
NSString *details = self.finalObject [#"Request"];
[self.detailsLabel setText:details];
NSLog(#"%#", self.finalObject);
// Configure interface objects here.
}
In IC, for .h I have:
#class DetailsForWatch;
#interface InterfaceController : WKInterfaceController {
DetailsForWatch *_theDetails;
}
#property (retain) DetailsForWatch *theDetails;
#end
In the .m I have:
#synthesize theDetails = _theDetails;
for didSelectRowAtIndex, I have:
_theObject = _theObjective[rowIndex];
self.theDetails = [[DetailsForWatch alloc] init];
_theDetails.finalObject = _theObject;
I set up the DFW as a Push selection from the Group on IC. When I select a row in the IC, it pushes a blank screen, and the NSLog shows that the PFObject named finalObject is (null). What am I doing wrong that it is not passing on PFObject properly?
There are a couple of ways to pass data between the two interface controllers. The way I have been doing it is like this:
create a segue (give it an identifier if necessary) between the two controllers in my storyboard.
In interface controller 1 implement
- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier
it will be called when the segue is triggered via button press or whatever.
This can return any object such as a dictionary of the data (in your case 'theDetails')
In interface controller 2 implement
- (void)awakeWithContext:(id)context
the context object here will be the one you passed through in controller 1

I need to make one instance of an array that can be access by multiple view controllers [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 9 years ago.
I need to make one instance of an array that can be access by multiple view controllers. It will contain objects that will be displayed in a table view and created by a modular view and then displayed in the table view. I can't figure out how to access it once I make a #property for it in the main view controller or the AppDelegate class.
You should make a singleton and the recommended way to do that in objective-c is to create a class and add a method that looks something like:
+ (YourClass *)sharedYourClass
{
static dispatch_once_t onceToken;
static YourClass *sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
Put the array as a property in your class.
//YourClass.h
#interface YourClass : NSObject
#property(nonatomic, strong)NSArray *yourArray;
+(YourClass *)sharedYourClass;
#end
And in every class you want to use your singleton start by importing YourClass.h and then use it like:
NSArray *arr = [YourClass sharedYourClass].yourArray;
[YourArray sharedYourClass].yourArray = [[NSArray alloc] init];
etc..
What I do is put the data I want shared, in your instance the array, in the AppDelegate. Then I define a protocol that the app delegate conforms to. This lets me access the data anywhere. For example, say I have an array I want everywhere:
First define a protocol:
#protocol ApplicationState<NSObject>
#property(nonatomic, strong) NSArray* data;
#end
Then make your app delegate conform to it:
#interface AppDelegate : UIResponder <UIApplicationDelegate, ApplicationState>
#property (strong, nonatomic) UIWindow *window;
#property(nonatomic, strong) NSArray* data;
#end
Then reading and writing this shared object is simple:
id<ApplicationState> appState = (id<ApplicationState>) [UIApplication sharedApplication].delegate;
appState.data = [[NSArray alloc] init];
I am going to assume you can make the class of the array inherit from NSObject, and then pass it to the View Controller from there...
You have 2 ways to do this:
1.- Instantiate 1 arra on the main class and pass the reference to each viewcontroller what you need.
2.- Make a singleton class to hold the array an use this in your project.
First create a class like this
//GlobalDataClass.h
#interface GlobalDataClass : NSObject
#property(nonatomic,retain)NSArray *myArray;
+(GlobalDataClass*)getInstance;
#end
#import "GlobalDataClass.h"
//GlobalDataClass.m
#implementation GlobalDataClass
#synthesize myArray;
static GlobalDataClass *instance =nil;
+(GlobalDataClass *)getInstance
{
#synchronized(self)
{
if(instance==nil)
{
instance = [GlobalDataClass new];
}
}
return instance;
}
#end
Then you can use it in your viewControllers like this:
-(void)viewDidLoad{
[super viewDidLoad];
self.dataObj = [GlobalDataClass getInstance];
NSLog(#"%#",self.dataObj.myArray);
}
Hope it helps!

How to pass NSObject from one view controller to another

I am using iOS 5 SDK with arc. I want to pass NSObject from VC1 view controller to VC2 view controller, do modification and put back into VC1 controller. I don't want to point same nsobject from both VC1 and VC2 controllers. Whenever I am passing nsobject, it should create a copy of that nsobject and do the modification. (Do not modify actual nsobject).
I have tried below code but it crashes and giving error as
-[ImageObject mutableCopyWithZone:]: unrecognized selector sent to instance 0x1364ec20
Code:
I have NSObject as :
#interface ImageObject : NSObject
#property (copy,nonatomic) NSString *path;
#property (nonatomic, readwrite) int id;
#end
In VC1 view controller:
I am passing my nsobject to VC2 view controller as follows:
VC2ViewController *vc2 = [[VC2ViewController alloc] initWithNibName:#"VC2ViewController" bundle:nil];
vc2.imageObj = [imgObj mutableCopy];
[self.navigationController pushViewController:vc2 animated:YES];
In VC2 view controller:
VC2ViewController.h file
#import "ImageObject.h"
#interface VC2ViewController : UIViewController
#property (retain,nonatomic) ImageObject *imageObj;
#end
VC2ViewController.m file
// modifying nsobject as below
-(void)modifyObject
{
UIViewController *previousViewController = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
if ([previousViewController isKindOfClass:[VC1ViewController class]])
{
VC1ViewController *parent = (VC1ViewController *) previousViewController;
if(parent != nil)
{
_imageObj.id = 2;
[parent reloadData:_imageObj];
}
parent = nil;
}
[self.navigationController popViewControllerAnimated:YES];
}
Have any idea ? How to resolve this issue?
Your ImageObject class needs to conform to the NSCopying Protocol.
This answer here explains better and shows you how the code looks like.
I also think that you need to use [imgObj copy] instead of [imgObj mutableCopy] because according to apple docs:
The NSMutableCopying protocol declares a method for providing mutable copies of an object.
Only classes that define an “immutable vs. mutable” distinction should adopt this protocol.
Classes that don’t define such a distinction should adopt NSCopying instead.

Getting View Controller to retrieve data from a data model file

I'm pretty new to programming. I am creating a very simple iOS Quiz app just for practice. Here is what I have gotten done thus far:
I created the Xcode project using the "Single View" template. Thus, I already have the appDelegate files, the View Controller and a View (XIB file).
My view only has four controllers: 2 UILabels and 2 UIButtons. Each button is paired up with a label. I have all the connections for these 4 controllers setup. When the user taps the button labelled "Get a State" it needs to populate it's label with the name of a state I have in an NSMutableArray called stateArray. When the user taps on the button labelled "Get Capital" is needs to populate it's label with the state's capital in it's label.
I created an Objective-C class that inherits from NSObject to hold my data model called dataModel. In the dataModel.m file I have created and populated the two arrays.
In the view controller .m file I imported the dataModel.h file.
The only problem I am having is getting the view controller to retrieve data from the dataModel file. I have read that I should probably be using Delegation, but I am just looking to know how to do it more simply...I ready something about the view controller and the data model file should have references to each other? If so, what would the coding look like?
Here is my coding thus far:
#import <UIKit/UIKit.h>
#interface onMyOwnViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *stateField;
#property (weak, nonatomic) IBOutlet UILabel *answerField;
- (IBAction)answerButton:(id)sender;
- (IBAction)stateButton:(id)sender;
#end
#import "onMyOwnViewController.h"
#import "dataModel.h"
#implementation onMyOwnViewController
- (IBAction)stateButton:(id)sender
{
NSString *myState = [stateArray objectAtIndex:0]; //this line produces an error.
[_stateField setText:myState];
[_answerField setText:#"hi"];
}
- (IBAction)answerButton:(id)sender
{
}
#end
Below is my dataModel coding:
#import <Foundation/Foundation.h>
#interface dataModel : NSObject
#property(nonatomic, strong) NSMutableArray *answerArray;
#property(nonatomic, strong) NSMutableArray *stateArray;
#end
#import "dataModel.h"
#import "onMyOwnViewController.h"
#implementation dataModel
- (id)init
{
self = [super init];
if(self){
_answerArray = [[NSMutableArray alloc]initWithObjects:#"Michigan", #"Illinios", nil];
_stateArray = [[NSMutableArray alloc]initWithObjects:#"Lansing", #"Springfield",
nil];
}
return self;
}
#end
When I run the app, everything works except retrieving data from the data model. When you replay, please reply with coding.
You need to create an instance of your dataModel in onMyOwnViewController.
#implementation onMyOwnViewController {
dataModel *data;
}
-(void)viewDidLoad {
data = [[dataModel alloc] init];
}
Then in your method call
- (IBAction)stateButton:(id)sender
{
NSString *myState = data.stateArray[0];
...
}

Where should I init a custom class in a viewcontroller in Objective-C?

What is the best practice and how would I use it in a method? I have an init method in my object's class already.
Where should I init a custom class in a viewcontroller in Objective-C?
you should initialize your variables or custom class object in viewDidLoad or in the initWithNibName method
how to use?
you could initialize the custom class object like this
CustomClass *classObj = [[CustomClass alloc] init];
how to use the custom class as property?
in MyViewController.h
#class CustomClass;
#interface MyViewController:ViewController
#property (strong,nonatomic)CustomClass *classObject;
in MyViewController.m
#import "CustomClass.h"
......
#synthesize classObject;
.....
self.classObject=[[CustomClass alloc]init];

Resources