Keep NSManagedObject outside of CoreData NSManagedObjectContext Block - objective-c

I use the a helper class to get a block with NSManagedObjectContext in it.
+ (void)openTheDocumentAndPerformBlock:(completion_handle_t)completionBlock;
And now I want to keep one of the NSManagedObject outside of the block, because I want to interact with it constantly. And if I can't keep it in the ViewController, I have to query it everytime when I need it, and it will be really troublesome.
1, And can I pass the NSManagedObject through segue to another VC?
2, does the UIManagedDocument has to be open all the time when those thing append?
3, And Can I edit the NSManagedObject outside of the block, and save it in the Block?

If I am understanding your question correctly, you just need an instance variable of type NSManagedObject in your class(the view controller class) so that you can assign your managed object, that you queried out in your block to this variable. You can use this wherever you want.As for your other questions:
1. Hope the following code snippet works:
In your destination view controller class declare an instance variable to store the passed object.
#property (strong) NSManagedObject *container;
inside the prepareForSegue: method of source view controller get the destination vc and pass the managed object:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIViewController *destVC = [segue destinationViewController];
[destVC setValue:<managed object from block> forKey:#"container"]
}
And yes you can use NSManagedObject inside and outside your block as long its the same managed object context you are using.
And I don't know the answer for your second question.

Related

How to use a variable from one class in another to show up on my next storyboard scene

I guess this has been asked before but I can't find something that I can use to answer my question and get this to work. So, here's what I have. In once class, I have the following:
IBOutlet UILabel *totalAmountLabel;
and I've also setup the following the score the total in:
#property (nonatomic, strong) NSString *finalTotal;
So I've set my amount to that String in one class, but how exactly do I get at it in another class? I assume I import my class, but once I've done that, what do I type to get at this variable? How would I declare an instance of my class to get at this variable, and even then, would I have access to it? Or do I go via the getters and setters that are done for me - if so how do I know what they're called?
I hope that makes sense, thanks in advance so your help.
I assume I import my class, but once I've done that, what do I type to get at this variable?
Unfortunately, it is not that simple: you need an object instance in order to set its instance variables. It does not matter if you go through properties or not, you need an instance either way.
The way you get that instance differs depending on how your classes interact. Since you are using ARC and IBOutlets, I am assuming that the totalAmountLabel is located in a view controller that you open using a segue. In this case, you need to import the destination controller's header in the starting controller's .m file, and add this code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"goToMyNextController"]) {
// Change the ID to match the one that you are using ^^^^^^^^^
MyNextViewController *nvc = [segue destinationViewController];
[nvc.totalAmountLabel setTitle:finalTotal forState:UIControlStateNormal];
}
you can use NsUser default
set you object with your keyword
[[NSUserDefaults standardUserDefaults]setObject:#"Ajay" forKey:#"name"];
than get that object any where in you project
NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:#"name"];

Passing a dictionary between two viewcontrollers?

I have an application in which I have a webservice call that returns data as a dictionary. I want to access this dictionary in another view controller for loading the values into a table.
Can anybody demonstrate how to pass this response dictionary from one view controller to another?
You could define an NSDictionary property in your AnotherViewController and set it from the previous controller. I'll give a brief example below.
//.h
#interface AnotherViewController {
NSDictionary *data;
}
#property(nonatomic,retain) NSDictionary *data;
#end
//.m
#implementation AnotherViewController
#synthesize data;
Now from the current controller, after initializing AnotherViewController you set the dictionary before presenting it.
AnotherViewController *controller = [[AnotherViewController alloc] init];
controller.data = myCurrentDictionary;
Now AnotherViewController has a data property with the value of the previous controller.
Hope this helps.
I am assuming that the webservice is called because something happened (button clicked, viewDidLoad/viewDidAppear). If this is the case, passing a reference of the UIViewController to the webservice class is a perfect valid option. Keep in mind that for this relationship you should create a protocol, so on your webservice class you have something like this:
id<ViewControllerResponseProtocol> referenceToViewController;
This ViewControllerResponseProtocolwould define a method like this:
-(void)responseFromWebservice:(NSDictionary*)myDictionary;
So when the webservice class has build the NSDictionary you can the above method from the referenceToViewController:
[referenceToViewController responseFromWebservice:myDictionary];
If there isn't any kind of relationship between both, you use could NSNotificationCenter for it.
P.S: The solution of skram is perfectly valid if you already have the NSDictionary from the webservice on the initial UIViewController and now you want to pass it to a new UIViewController. Although I don't think that's what you want.

Load custom class properly

I have a custom class which I want to "load" inside the firstViewController and then access it from other classes by segues. My Problem is, I can't even access and change the instance variable inside the firstViewController. Somehow I'm "loading" it wrong. Here is the code I used until now:
inside viewController.h
#property (strong, nonatomic) myClass *newClass;
inside viewController.m
#synthesize newClass;
I then try to access it by:
self.newClass.string = #"myString";
if(newClass.string == #"myString"){
NSLog(#"didn't work");
}
Well, I get "didn't work". Why is that?
When I write
myClass *newClass = [myClass new];
It does work. But the class and its properties gets overwritten every time the ViewController loads again.
What would you recommend? Thank you very much.
Like Kaan said, you forgot to initialize your class, You have only declared and created a pointer for it but not the actual object, on your ViewDidLoad add
self.newClass = [[myClass alloc] init];
It does work. But the class and its properties gets overwritten every
time the ViewController loads again.
That's because every time that specific Viewcontroller loads you are reinitializing the class.
If you want a persistent class through all your program look for the singleton pattern.
This is used in the case when you want to have only 1 instance of a certain object, if you try to initialize another instance of that object you will just receive the one you already have.
PD: newClass.string == #"myString" is wrong.
Use the isEqualToString method when comparing strings.

NSArray is empty when accesing in a method of the UITableViewController

I'm trying to do a UITableView, but I'm totally stuck. My problem is I'm trying to do the method -tableView:cellForRowAtIndexPath: and for filling the cells I have a NSArray of myObject, wich is set by another class via a setter in my UITableViewController.
My problem is that when I'm gonna use that array, it is totally empty, and I donĀ“t know why -- when I do the setter, the object has data, but when I try to do the getter inside the method above, it is empty. Can someone tell me why, and how can I fix it?
My setter:
- (void)setmyObjectList:(NSArray *)list{
_myObjectList = list;
}
MyObjectList is declared in the .h:
#property (nonatomic, strong) NSArray *myObjectList;
And this code is in the Controller that call my TableViewController via a segue:
myTableViewController *myTVC = segue.destinationViewController;
[myTVC setPharmaciesList:self.list];
Maybe i didn't explain well: I have a class that has a NSArray with the the objects I wanna show in my TableView. So via a segue I set this NSArray in the TableViewController. In the setter when I do a NSLog all the data is in the NSArray in the property of the NSTableViewController, but when I try to access in the method tableView:cellForRowAtIndexPath: the info is empty. Why? I have no idea.
Thanks :P
First, don't override the setter method since youre not doing anything special there. Second, where do you create the array that you later set your property to by calling
[myTVC setPharmaciesList:self.list];
I.e. where do you populate self.list?

What's an easy way to set up object communication in Obj-C?

I am trying to send a slider value from a controller object to a method of a model object. The later is implemented in the separate file and I have appropriate headers. I think the problem is that I am not sure how to instantiate the receiver in order to produce a working method for the controller.
Here is the controller's method.
-(IBAction)setValue:(id)slider {[Model setValue:[slider floatValue]];}
#implementation Model
-(void)setValue:(float)n{
printf("%f",n);
}
#end
What I get is 'Model' may not respond to '+setValue' warning and no output in my console.
Any insight is appreciated.
you should allocate the modal first because the method is an instance method and cannot be used as an class(static) method.
Use Model *modelObject = [[Model alloc] init];
[modelObject setValue:2];
Another way to do this, if this makes sense for your project, is to 1) in your nib file add an object controller 2) set the class of that object controller to your model class 3) in your model class create an instance variable for the slider: float sliderFloatValue 4) create accessors for the float :#property (readwrite, assign) float sliderFloatValue; #synthesize sliderFloatValue; 5) bind the slider value to the sliderFloatValue of your model class