0

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

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

1 Answers1

1

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

Community
  • 1
  • 1
Krishna
  • 770
  • 2
  • 8
  • 21