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?
Related
Edit: the first answer has guessed it is because I am not using a protocol to "pass data backwards." I am currently implementing that and we'll see if it fixes it.
I'm very new to iOS/objective-C. I have this property on my class:
#interface GameController
#property (strong, nonatomic) NSMutableArray* targets;
#end
I was then using addObject on the array in the body of one of the GameController methods, and it didn't throw any errors, so I thought it was okay until I checked [_targets count] at the end of that method and found it was zero. So I initialized it at the beginning of that method:
if(self.targets == nil){
self.targets = [[NSMutableArray alloc] init];
}
And then count was 6. But as soon as I go to read self.targets in a different method, it's back to zero. Why is that happening? What am I doing wrong with initializing this?
Edit: Interestingly, in the other method, if I check, it is no longer nil-- it has been initialized. But it is nonetheless empty. Very confused!
Edit2: Okay, I've uploaded the code to pastebin, having cut out irrelevant stuff as much as possible.
So drawMainControls gets called first, when the app loads, and it draws a bunch of TileViews and TargetViews, instantiating self.targets in the process. Each TileView has a GameController property which is assigned to be the main GameController immediately after instantiation.
The TileView detects taps and calls addPossibleLetter on its controller. addPossibleLetter is the method that checks the self.targets array and comes up empty-handed.
Here is the GameController.h file: http://pastebin.com/C30Mi2LL
Here is the GameController.m file: http://pastebin.com/cvf2WmBs
Here is the TileView.m file: http://pastebin.com/gehJGYBD
There are fair chances that you are calling the other method before the method in which this method is being initialized.
I guess when passing data backward the way you are doing you are properly aware of using protocols and delegates. You can have look at his link which explains this concept very clearly. Passing Data between View Controllers
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.
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"];
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.
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.