Hi I have an iphone application in which I am fetching & parsing data in in applicationDidFinishLaunching. Now I want to transfer this fetched data which is in one NSMutableArray to my first view controller to display it there.
Whats the best way of doing this...
You can past it through the init like initWithDataArray: , I think that is good enough.
Another solution, which imo is worse, is parsing the AppDelegate to the ViewController and then you can call : appDelegate.dataArray but it will leak out too much information
Application Delegate is what i prefer in this case.
//Declare NSMutableArray object in AppDelegate.h ,Now property & synthesize it
//Store your data in NSMutableArray object
//Create ApplicationDelegate object any your View Controller Class
yourApplicationAppDelegate *appDelegate;
appDelegate = (yourApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
Now you can access NSMutableArray object through appDelegate any where in your application
Related
My App need to save some data (around 100 double fields that is stored in array and some few other string data as well). I have a method -(void) case2save already in place to save data to the NSDocumentDirectory when the user initiates it and all is working well, however I need to save the same data when the App is terminated either by the user (home) or by the iOS.
I'm trying to use the applicationDidEnterBackground from the delegate to call that same method i use in the view controller .m class but since the function is per the object (not class method) i can't, but then if i'm making it to be class method i don't have the actual data which is part of the object... so that's the issue.
An idea how to resolve that issue and save the data when App is terminated or goes to the background ?
Thanks.
you can make that function in appdelegate , or just declare a data member of that class in appdelegate.h and make it property and set this data member as self in that function where you are trying to save data , use following.....
//in Your Appdelegate.h suppose you have viewController named AbcViewController ....
AbcViewController *abcVC;
create property ...
#property (nonatomic,retain) AbcViewController *abcVC;
//in Your Appdelegate.m
#synthesize abcVC;
//Now in function where you are trying to save database .....
AppdelegateClass *app = (AppdelegateClass*)[[UIApplication sharedApplication]delegate];
app.abcVC = self;
//now call that function from applicationDidEnterBackground with the help of this object...
Hope this will resolve your issue...
There is a notification sent, called UIApplicationDidEnterBackgroundNotification. You can listen for that in your view controller and do whatever you need in response.
I have created a project where Core Data is included. If I were to retrieve data from another class other than my AppDelegate - where all the needed methods are - how would I do that ? So how do I get the context and the NSManagedObjectModel ? Do I need those, or is there another way round ?
I have tried making a new NSObjectContext object in my new class but then the debugger says it needs the NSManagedObjectModel. I am a newbie and I'd appreciate any help.
Yes, you need the NSManagedObjectContext. Basically what you have to do is inject the managedobjectmodel in the class where you want to query the database.
So in the class where you want to query the database create a property like this:
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
And then do in your app delegate:
myViewController.managedObjectContext = self.managedObjectContex;
If this view controller does not have a reference within the app delegate then you can do this:
MyAppDelegate appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
myViewController.managedObjectContext = appDelegate.managedObjectContex;
Hope that helps.
I have a RootViewController and a DetailViewController where I am trying to pass a NSMutableArray from the rootView to the detailView. I have created an object of the RootViewController inside the DetailViewController and accessing it like the following
RootViewController *root = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
detailViewArray = [root.rootViewArray copy];
Note: Both the arrays are declared in the .h files; synthesized and then allocated and initialized array = [[NSMutableArray alloc] init];
Problem: I am not too sure why it still doesn't work. I have tried a lot of the solutions on the internet but it didn't quite work for me. The property for the root array is nonatomic, retain Is it something wrong with that? Do I need to change it to something or the method I am following is just not right.. Please if someone could help!
If you alloc/init the RootViewController inside the DetailViewController, you are creating another instance of the RootViewController. You are not getting the same instance (with data) of the rootViewController.
That said, even passing a reference of a viewController to another viewController to then poke at it's data is sort of bad. It creates tight coupling between the views.
Instead, if you need to get data, consider using a delegate to communicate between the views.
What exactly does delegate do in xcode ios project?
Tutorial:
http://www.theappcodeblog.com/2011/04/15/passing-data-between-views-tutorial-using-a-protocol-delegate-in-your-iphone-app/
Another option is to create a shared model (read up on model view controller patterns). It's typical in that pattern to create a model and share data by getting a singleton instance of your model:
MyModel *model = [MyModel sharedInstance];
Then, each view can set and read data from that same (singleton) instance of the model.
Which to choose? The model is better if many views share the same data. A delegate is appropriate for a couple views to communicate with each via callbacks.
Even there is an another way to pass array.
MyViewController mvc = [[MYViewController alloc] initWithNibName:#"MYViewController" bundle:[NSBundle mainBundler]];
[mvc getDataArray:<pass your array>];
[self.navigationController pushViewController:mvc animated:YES];
Here it will set a dataArray property first then it will push the view controller to MyViewController class.
This will be useful when you are very much concerning about memory. Because if you are using static (sharedInstance) then its scope persistance within the entire application scope.
I'm pretty new to iPhone development.
I am building an app which has multiple views and controllers. There is only one model.
I need to share the model amongst all of the controllers; so I have instantiated the model inside the App Delegate header file:
#interface MyAppDelegate
(...snip...)
#property (nonatomic, retain) CalcModel *model;
and then synthesized it accordingly.
Inside a controller, I have tried to reference the model like so:
CalcModel* model = [[[UIApplication sharedApplication] delegate] model];
The problem is that the compiler says '-model' not found in protocol
This is probably because the delegate field returns the protocol type, not the concrete MyAppDelegate type... so should I cast [[UIApplication sharedApplication] delegate] to MyAppDelegate, so I can access the model property? If so, how?
Or is this all wrong? More broadly, how would you share a model amongst view controllers?
many thanks for your help
Yes, just cast it. I #define a macro that wraps this into a simple call to make it easier.
With regard to one way to implement the model structure, there are some useful pointers in this article:
http://www.bit-101.com/blog/?p=1969
(We just implement models as singletons themselves, and use KVO from the views to listen for changes to properties.)
You can typecast it just like you typecast any other object
CalcModel* model = (MyAppDelegate *)[[[UIApplication sharedApplication] delegate] model];
you'll have to instanciate the model in you appdelegate .m
I would like to acces to a object in appdelegate from diferent views. I mean, in the appdelegate I have a object which I need in diferent view but I don't know how get the instance.
In my app, I have only one object with information from the user and I need this data from diferents views.
yourObject* obj = [(YourAppDelegate*)[[UIApplication sharedApplication] delegate] yourObject];