Why doesn't my UIViewController class keep track of an NSArray instance variable.
Posted
by TaoStoner
on Stack Overflow
See other posts from Stack Overflow
or by TaoStoner
Published on 2010-06-10T18:25:18Z
Indexed on
2010/06/10
18:32 UTC
Read the original article
Hit count: 216
Hey, I am new to Objective-C 2.0 and Xcode, so forgive me if I am missing something elementary here. Anyways, I am trying to make my own UIViewController class called GameView to display a new view. To work the game I need to keep track of an NSArray that I want to load from a plist file. I have made a method 'loadGame' which I want to load the correct NSArray into an instance variable. However it appears that after the method executes the instance variable loses track of the array. Its easier if I just show you the code....
@interface GameView : UIViewController {
IBOutlet UIView *view
IBOutlet UILabel *label;
NSArray *currentGame;
}
-(IBOutlet)next;
-(void)loadDefault;
...
@implementation GameView
- (IBOutlet)next{
int numElements = [currentGame count];
int r = rand() % numElements;
NSString *myString = [currentGame objectAtIndex:(NSUInteger)r];
[label setText: myString];
}
- (void)loadDefault {
NSDictionary *games;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Games.plist"];
games = [NSDictionary dictionaryWithContentsOfFile:finalPath];
currentGame = [games objectForKey:@"Default"];
}
when loadDefault gets called, everything runs perfectly, but when I try to use the currentGame NSArray later in the method call to next, currentGame appears to be nil. I am also aware of the memory management issues with this code. Any help would be appreciated with this problem.
© Stack Overflow or respective owner