Newbie error?
Im working on a quiz app that uses answer results from a view contoller, creates NSMutableArray objects for totalCorrect and totalTried values in a KeepScore model .m file and the model returns a score back to my view controller. The view controller then updates my score label in my view. I then use the UINavigationController to segue to my next view controller and then wait for the user to input the next quiz question answer. Problem is that the next time i call my KeepScore model .m file from my view controller 2, the NSMutableArray objects in KeepScore model .m have reverted to Null values. I made sure my NSMutableArray object values were correct (non Null) right before returning from my KeepScore .m model to my view controller 1. Im working iOS 5.1 with Xcode 4.3.1 with ARC. My NSMutableArray property in my private API declaration in .m model file is strong. Shouldn’t the KeepScore model .m keep my array object values when i return to my view controller so that i can use them again when i call my model from my next view controller? I wanted to ask in wording first to see if anyone catches any syupid logic I have. I’ll post code if helpful.
Here is my KeepScore.h model
#import
@interface KeepScore : NSObject
- (float)score:(BOOL)questionCorrect:(BOOL)questionTotal:(BOOL)firstScoringPass;
@end
Here is my KeepScore.m model implementation:
#import "KeepScore.h"
@interface KeepScore()
@property (nonatomic, strong) NSMutableArray *correctScoreValues;
@end
@implementation KeepScore
@synthesize correctScoreValues = _correctScoreValues;
- (NSMutableArray *)correctScoreValues
{
if (_correctScoreValues == nil) _correctScoreValues = [[NSMutableArray alloc] init];
return _correctScoreValues;
}
- (float)score:(BOOL)questionCorrect:(BOOL)questionTotal:(BOOL)firstScoringPass {
int totalTried;
int totalCorrect;
NSLog(@"firstScoringPass = %@", firstScoringPass ? @"YES" : @"NO");
NSLog(@"questionCorrect = %@", questionCorrect ? @"YES" : @"NO");
NSLog(@"questionTotal = %@", questionTotal ? @"YES" : @"NO");
if (firstScoringPass) {
int totalTriedInt = 0;
int totalCorrectInt = 0;
NSNumber *correct = [NSNumber numberWithInt:totalCorrectInt];
NSNumber *tried = [NSNumber numberWithInt:totalTriedInt];
[self.correctScoreValues addObject:correct];
[self.correctScoreValues addObject:tried];
firstScoringPass = NO;
}
if (questionCorrect) {
totalCorrect = ([[self.correctScoreValues objectAtIndex:0] intValue] + 1);
NSLog(@"totalCorrect = %d", totalCorrect);
NSNumber *correct = [NSNumber numberWithInt:totalCorrect];
NSLog(@"correct = %@", correct);
[self.correctScoreValues replaceObjectAtIndex:0 withObject:correct];
NSLog(@"correct value in array = %d",[[self.correctScoreValues objectAtIndex:0] intValue]);
} else {
totalCorrect = [[self.correctScoreValues objectAtIndex:0] intValue];
}
NSLog(@"totalCorrect = %d", totalCorrect);
if (questionTotal) {
NSLog(@"tried value in array = %d", [[self.correctScoreValues objectAtIndex:1] intValue]);
totalTried = ([[self.correctScoreValues objectAtIndex:1] intValue] + 1);
NSNumber *tried = [NSNumber numberWithInt:totalTried];
[self.correctScoreValues replaceObjectAtIndex:1 withObject:tried];
NSLog(@"tried value in array = %d", [[self.correctScoreValues objectAtIndex:1] intValue]);
}
float score = (totalCorrect/totalTried);
NSLog(@"score = %0.2f", score);
NSLog(@"totalCorrect = %d, totalTried = %d", totalCorrect, totalTried);
NSLog(@"correct value in array = %d", [[self.correctScoreValues objectAtIndex:0] intValue]);
NSLog(@"tried value in array = %d", [[self.correctScoreValues objectAtIndex:1] intValue]);
return score;
}
@end
Here is one of my ViewControllers .m files:
#import "MapQuizViewController.h"
#import "KeepScore.h"
@interface MapQuizViewController()
@property (nonatomic, strong) KeepScore *scoreModel;
@property (nonatomic) BOOL questionTotal;
@property (nonatomic) BOOL questionCorrect;
@property (nonatomic) BOOL firstScoringPass;
@end
@implementation MapQuizViewController
@synthesize gradeLabel = _gradeLabel;
@synthesize scoreLabel = _scoreLabel;
@synthesize scoreModel = _scoreModel;
@synthesize questionTotal = _questionTotal;
@synthesize questionCorrect = _questionCorrect;
@synthesize firstScoringPass = _firstScoringPass;
- (KeepScore *)scoreModel;
{
if (!_scoreModel) _scoreModel = [[KeepScore alloc] init];
return _scoreModel;
}
- (IBAction)answerButtonPressed:(UIButton *)sender
{
NSString *answerChoice = sender.currentTitle;
NSString *correct = [NSString stringWithFormat:@"Correct!"];
NSString *incorrect = [NSString stringWithFormat:@"Incorrect"];
self.questionTotal = YES;
self.firstScoringPass = YES;
if ([answerChoice isEqualToString:@"Australia"]) {
self.gradeLabel.textColor = [UIColor blueColor];
self.gradeLabel.text = [NSString stringWithFormat:@"%@",correct];
self.questionCorrect = YES;
self.scoreLabel.text = [NSString stringWithFormat:@"%0.2f",[self.scoreModel score:_questionCorrect :_questionTotal :_firstScoringPass]];
} else if ([answerChoice isEqualToString:@"U.S."]) {
self.gradeLabel.textColor = [UIColor redColor];
self.gradeLabel.text = [NSString stringWithFormat:@"%@", incorrect];
self.questionCorrect = NO;
self.scoreLabel.text = [NSString stringWithFormat:@"%0.2f",[self.scoreModel score:_questionCorrect :_questionTotal :_firstScoringPass]];
} else if ([answerChoice isEqualToString:@"China"]) {
self.gradeLabel.textColor = [UIColor redColor];
self.gradeLabel.text = [NSString stringWithFormat:@"%@", incorrect];
self.questionCorrect = NO;
self.scoreLabel.text = [NSString stringWithFormat:@"%0.2f",[self.scoreModel score:_questionCorrect :_questionTotal :_firstScoringPass]];
}
}
- (void)viewDidUnload {
[self setScoreLabel:nil];
[super viewDidUnload];
}
@end
First, a single .m file means nothing. I think you assumed that there is one instance of a Class per .m file which is not true.
for example
If you didn’t pass your KeepScore instance around, newly created one will not have previous one’s data.
You have to either to make KeepScore singleton or pass it from old view controller to new view controller