Objective-C How to add objects to NSMutableArray from other place? - ios

Hello I'm junior in Objective-C and Swift programming.
I have NSMutableArray in ExampleMenuViewController.m or(and) SomeClass.m declared as vcTabs.
NSMutableArray *vcTabs;
When I have two declares 'vcTabs' Xcode returns duplicate symbol '_vcTabs' (...)
How to add objects to an existing NSMutableArray init in other class (ExampleMenuViewController.m)?
I need append new objects from another class (SomeClass.m) to vcTabs (NSMutableArray).
I wrote in SomeClass.m this code:
if ([Tools isNonullValueForKey:[dictionary valueForKey:#"additional_tabs"]]) {
additional_tabs = [dictionary valueForKey:#"additional_tabs"];
NSLog(#"additionalTabs count: %lu", [additional_tabs count]);
for (int i = 0; i < [additional_tabs count]; i++) {
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:#"_id"]]) {
additional_tab_id = [[additional_tabs valueForKey:#"_id"] objectAtIndex:i];
}
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:#"names"]]) {
NSDictionary *dic = [[additional_tabs valueForKey:#"names"] objectAtIndex:i];
_en_additional_tab_name = [dic valueForKey:#"en"];
_pl_additional_tab_name = [dic valueForKey:#"pl"];
}
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:#"url"]]) {
additional_tab_url = [[additional_tabs valueForKey:#"url"] objectAtIndex:i];
//NSLog(#"additional_tab_url: %#", _additional_tab_url);
}
[vcTabs addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additional_tab_id :VCTabAdditional :additional_tab_url]];
NSLog(#"%# %d %# %# %# %#", #"pos", i, #"id: ", additional_tab_id, #"url: ", additional_tab_url);
}
}
ExampleMenuViewController method with initVCTabs
- (void)initVCTabs {
vcTabs = [[NSMutableArray alloc] init];
[vcTabs removeAllObjects];
if ([Tools getBooleanUserDefault:#"example_visible_tab_attendees" :YES]) {
[vcTabs addObject:[[VCTab alloc] initWithType:VCTabAttendees]];
}
(...)
if ([Tools getBooleanUserDefault:#"example_visible_tab_user_info" :YES]) {
[vcTabs addObject:[[VCTab alloc] initWithType:VCTabUserInfo]];
}
if ([Tools getStringUserDefault:#"example_additional_tab_id" :#""]) {
NSString *additionalTabId = [Tools getStringUserDefault:#"conference_additional_tab_id" :#""];
NSString *additionalTabUrl = [Tools getStringUserDefault:#"conference_additional_tab_url" :#""];
NSLog(#"additionalTabId %#", additionalTabId);
NSLog(#"additionalTabUrl %#", additionalTabUrl);
[vcTabs addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additionalTabId :VCTabAdditional :additionalTabUrl]];
}
}
PS. If I use from ExampleMenuViewController I have only one tab with last object properties... but additional_tabs array have 17 objects.
Do you have any ideas or advices?
All the best for you everyone!

When are you calling initVCTabs?
When / how is the code in SomeClass.m running?
For being a "junior in Objective-C and Swift programming" you seem to have a lot going on that you don't understand yet. Try creating a new project and learn how things work -- then implement that in your full project.
Here is a very, very simple example. With the information you provided in your question, this may or may not relate directly - but it should give you an idea of where to go:
SomeClass.h
// SomeClass.h
// Created by Don Mag on 8/30/20.
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
#interface SomeClass : NSObject
- (void)moreTabs:(NSMutableArray *)a;
#end
NS_ASSUME_NONNULL_END
SomeClass.m
// SomeClass.m
// Created by Don Mag on 8/30/20.
#import "SomeClass.h"
#interface SomeClass()
#end
#implementation SomeClass
- (void)moreTabs:(NSMutableArray *)a {
[a addObject:#"B"];
[a addObject:#"C"];
[a addObject:#"D"];
[a addObject:#"E"];
}
#end
ExampleMenuViewController.h
// ExampleMenuViewController.h
// Created by Don Mag on 8/30/20.
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface ExampleMenuViewController : UIViewController
#end
NS_ASSUME_NONNULL_END
ExampleMenuViewController.m
// ExampleMenuViewController.m
// Created by Don Mag on 8/30/20.
#import "ExampleMenuViewController.h"
#import "SomeClass.h"
#interface ExampleMenuViewController ()
{
NSMutableArray *vcTabs;
}
#end
#implementation ExampleMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
// add a button to the view
//UIButton *b = [UIButton new];
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
[b setTitle:#"Tap Me" forState:UIControlStateNormal];
b.frame = CGRectMake(0, 0, 200, 50);
b.center = self.view.center;
[self.view addSubview:b];
[b addTarget:self action:#selector(btnTapped) forControlEvents:UIControlEventTouchUpInside];
[self initVCTabs];
[self logArray];
}
- (void)initVCTabs {
// instantiate NSMutableArray
vcTabs = [NSMutableArray new];
// add one object
[vcTabs addObject:#"A"];
}
- (void)btnTapped {
SomeClass *sc = [SomeClass new];
[sc moreTabs:vcTabs];
[self logArray];
}
- (void)logArray {
NSLog(#"vcTabs has %ld objects", [vcTabs count]);
for (NSString *s in vcTabs) {
NSLog(#"%#", s);
}
}
#end
When ExampleMenuViewController loads, it will add a button to the center of the view, then instantiate the vcTabs array and add one object - #"A".
We log the array to the debug console and see:
vcTabs has 1 objects
A
When you tap the button, an instance of SomeClass will be created, we call the moreTabs method in that class, passing a reference to vcTabs. That method will add 4 objects to the array - #"B" #"C" #"D" #"E".
We then log the array to the debug console and see:
vcTabs has 5 objects
A
B
C
D
E

#DonMag
It's working but I have one question to you. I added for loop to increment. When I add [additional_tabs count] is return nil. Why this method adds the same objects? My code in addMore function:
- (void)moreTabs:(NSMutableArray *)a {
for (int i = 1; i < additional_tab_count; i++) {
NSString *additionalTabId = [Tools getStringUserDefault:#"conference_additional_tab_id" :#""];
NSString *additionalTabUrl = [Tools getStringUserDefault:#"conference_additional_tab_url" :#""];
[a addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additionalTabId :VCTabAdditional :additionalTabUrl]];
}
}
Do you know how to get THIS "dictionary" or "additional_tabs". I tried but always return nil (looks like a new other instance and is not this dictionary or additional_tabs from initFromJSON). I only managed to make variable additional_tab_count and it's return 17 (correct count), but I'd rather have access to array from additional_tabs declared in if statements.

Related

Issue with iOS 7 Matchismo assignment 1 Stanford

I've written code for the first assignment in the iTunesU course on iOS 7 programming from Stanford University. The problem is, when I compile I get a warning that says Method definition for addCard: not found.
This is the code I have in the Deck class. Can anyone tell me what I'm doing wrong?
#import "Deck.h"
#interface Deck()
#property(strong, nonatomic)NSMutableArray *cards;// of Card
#end
#implementation Deck
-(NSMutableArray *)cards
{
if(!_cards) _cards = [[NSMutableArray alloc] init];
return _cards;
}
-(void)addCard:(Card *)card atTop:(BOOL)atTop
{
if(atTop){
[self.cards insertObject:card atIndex:0];
}else{
[self.cards addObject:card];
}
}
-(void)addcard:(Card *)card
{
[self addCard:card atTop:NO];
}
-(Card *)drawRandomCard
{
Card *randomCard = nil;
if ([self.cards count]) {
unsigned index = arc4random() % [self.cards count];
randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
}
return randomCard;
}
#end
In your .h file, you've named the method addCard:, but in your .m file, you've spelled the method addcard:.
Objective-C is case-sensitive.
Change this line in your .m file:
-(void)addcard:(Card *)card
To look like this:
-(void)addCard:(Card *)card

Calling a method from a different view controller

I've been exploring virtual card deck randomisation and drawing of cards as a little side hobby recently, and have been focussing on a great tutorial found at Ontswedder. However, I've run into a minor issue which I'm hoping you can help me with!
Below, I've included some key aspects of the code for your viewing.
In my viewController, I'm wanting to call both the shuffle and draw methods, found in Deck.m. This is possible if I do the following:
Deck *d = [[Deck alloc] init];
[d shuffle];
[d deal];
However, I need to be able to split these. For example, clicking one button initialises the deck, and shuffles it, whilst another new button draws a new card (until the deck is empty).
I am having issues with this, and I could do the following:
Deck *d = [[Deck alloc] init];
[d shuffle];
Deck *d = [[Deck alloc] init];
[d deal];
But this is useless to me, as if I call the init then a NEW, UNSHUFFLED deck is created and the card is dealt from this new deck.
I've tried declaring Deck *DeckObject in viewController.h, then simply calling [DeckObject draw];, however this provides a null value.
Ultimately, I would like to call these two methods separately. Without needing to init a new deck every time if possible!! :-)
I greatly appreciate both everyone's help on this post but also site-wide!
I hope this makes sense, too. Please let me know if you would like more information.
Also, I apologise if any of my terminology is incorrect - still a bit of a newbie!
Included code for context:
Card.h
typedef enum {
Hearts,
Diamonds,
Spades,
Clubs
} Suit;
#define Ace 1
#define Jack 11
#define Queen 12
#define King 13
#interface Card : NSObject {
NSInteger value;
Suit suit;
}
#property (nonatomic) NSInteger value;
#property (nonatomic) Suit suit;
- (id) initWithValue:(NSInteger) aValue suit:(Suit) aSuit;
#end
Card.m
#interface Card(Private)
- (NSString *) valueAsString;
- (NSString *) suitAsString;
#end
#implementation Card
#synthesize value,suit;
- (id) initWithValue:(NSInteger) aValue suit:(Suit) aSuit {
if(self = [super init]) {
self.value = aValue;
self.suit = aSuit;
}
return self;
}
- (NSString *) valueAsString {
switch (self.value) {
case Ace:
return #"Ace";
break;
case Jack:
return #"Jack";
break;
case Queen:
return #"Queen";
break;
case King:
return #"King";
break;
default:
return [NSString stringWithFormat:#"%d",self.value];
break;
}
}
- (NSString *) suitAsString {
switch (self.suit) {
case Hearts:
return #"Hearts";
break;
case Diamonds:
return #"Diamonds";
break;
case Spades:
return #"Spades";
break;
case Clubs:
return #"Clubs";
break;
default:
return nil;
break;
}
}
- (NSString *) description {
return [NSString stringWithFormat:#"%# %#",
[self valueAsString],
[self suitAsString]];
}
#end
Deck.h
#interface Deck : NSObject {
#private
NSMutableArray *cards;
}
- (void) shuffle;
- (Card *) draw;
- (NSInteger) cardsRemaining;
#end
Deck.m
#implementation Deck
- (id) init {
if(self = [super init]) {
cards = [[NSMutableArray alloc] init];
for(int suit = 0; suit <= 3; suit++) {
for(int value = 1; value <= 13; value++) {
Card *card = [[Card alloc] initWithValue:value suit:suit];
[cards addObject:card];
[card release];
}
}
}
return self;
}
int randomSort(id obj1, id obj2, void *context ) {
// returns random number -1 0 1
return (arc4random()%3 - 1);
}
- (void) shuffle {
for(int x = 0; x < 500; x++) {
[cards sortUsingFunction:randomSort context:nil];
}
}
- (Card *) draw {
if([self cardsRemaining] > 0) {
Card *card = [[cards lastObject] retain];
[cards removeLastObject];
return [card autorelease];
}
NSException* myException = [NSException
exceptionWithName:#"OutOfCardsException"
reason:#"Tried to draw a card from a deck with 0 cards." userInfo:nil]; #throw
myException;
}
- (NSInteger) cardsRemaining {
return [cards count];
}
- (NSString *) description {
NSString *desc = [NSString stringWithFormat:#"Deck with %d cards\n",[self
cardsRemaining]];
for(int x = 0; x < [self cardsRemaining]; x++) {
desc = [desc stringByAppendingFormat:#"%#\n",[[cards objectAtIndex:x] description]];
}
return desc;
}
- (void) dealloc {
[cards release];
[super dealloc];
}
#end
When you declare the property deckObject, that just creates the pointer, you still have to alloc init a Deck object to create an instance.
self.deckObject = [[Deck alloc] init];
Then, you can use [self.deckObject shuffle] and [self.deckObject deal]in different button methods.
you have to declaring Deck *DeckObject in viewController.h as below
#interface firstViewController : UIViewController {
Deck *DeckObject;
}
Now in viewController.m file's viewDidLoad method initialize it like below
Deck *DeckObject = [[Deck alloc] init];
after that you can call methods in uibutton's action method
- (IBAction)BtnShuffle:(id)sender
{
if (DeckObject) {
[DeckObject shuffle];
}
}
and
- (IBAction)BtnDraw:(id)sender
{
if (DeckObject) {
[DeckObject draw];
}
}

No visible #interface for 'BinaryFileReader' declares the selector 'initWithLocation:'

I am having an this error come up:
No visible #interface for 'BinaryFileReader' declares the selector 'initWithLocation:'
for every method called on BinaryFileReader in the method below.
+ (Item*)loadItem:(NSString*)filepath {
Item* newItem = [[Item alloc] init];
BinaryFileReader* input = [[BinaryFileReader alloc] initWithLocation:filepath];
[newItem setName:[input readNSString]];
[newItem setOtherNames:[input readNSMutableArrayOfNSString]];
[newItem setDescription:[input readNSString]];
[newItem setXactCode:[input readNSString]];
[newItem setSymbilityCode:[input readNSString]];
[newItem setAverageLowPrice:[input readInt]];
[newItem setAverageHighPrice:[input readInt]];
[newItem setAverageLifeExpectancy:[input readInt]];
return newItem;
}
Here is my BinaryFileReader.m:
#import "BinaryFileReader.h"
#implementation BinaryFileReader
- (id)init {
self = [super init];
return self;
}
- (id)initWithLocation:(NSString*)filepath {
if ((self = [super init])) {
_file = [NSFileHandle fileHandleForReadingAtPath:filepath];
_fileOffset = 0;
if (_file == nil)
NSLog(#"%#%#",#"Failed to open file at path:",filepath);
}
return self;
}
- (void)close {
[_file closeFile];
}
- (int)readInt {
[_file seekToFileOffset:_fileOffset];
_databuffer = [_file readDataOfLength:4];
_fileOffset+=4;
return (*(int*)([_databuffer bytes]));
}
- (NSString*)readNSString {
int length = [self readInt];
[_file seekToFileOffset:_fileOffset];
_databuffer = [_file readDataOfLength:length];
_fileOffset+=length;
return [[NSString alloc] initWithData:_databuffer encoding:NSUTF8StringEncoding];
}
- (NSMutableArray*)readNSMutableArrayOfNSString {
NSMutableArray* array = [[NSMutableArray alloc] init];
int arrayLength = [self readInt];
int length;
for (int i=0; i<arrayLength; i++) {
length = [self readInt];
[_file seekToFileOffset:_fileOffset];
_databuffer = [_file readDataOfLength:length];
_fileOffset+=length;
[array addObject:[[NSString alloc] initWithData:_databuffer encoding:NSUTF8StringEncoding]];
}
return array;
}
#end
And BinaryFileReader.h:
#interface BinaryFileReader : NSObject
#property (nonatomic) int fileOffset;
#property (nonatomic, retain) NSData* databuffer;
#property (nonatomic, retain) NSFileHandle* file;
- (id)init;
- (id)initWithLocation:(NSString*)filepath;
- (void)close;
- (int)readInt;
- (NSString*)readNSString;
- (NSMutableArray*)readNSMutableArrayOfNSString;
#end
I don't understand why I am having this issue. Any help to clear this up would be much appreciated.
I copy&pasted the code from your question into a project of mine. Expected result: Everything compiles fine. So it's definitely not your code that is the problem.
Coming back to the idea of a different BinaryFileReader.h polluting your #imports: Do you use any third party libraries in your project? If yes, one of those might unexpectedly provide a BinaryFileReader.h file.
Try this: Rename BinaryFileReader.h to something else, e.g. Foo.h. Also change the #import in BinaryFileReader.m to use Foo.h, but leave the #import in the file where your loadItem: method is located so that it still uses BinaryFileReader.h. Now compile again. The compiler should now complain that it can't find BinaryFileReader.h. If the error is still about the missing selector then you know that somewhere you have an unexpected BinaryFileReader.h messing up your project.

Unexpected result when overriding a method of a class cluster

While implementing a subclass of NSArray (a class cluster), I was surprised to see that my overridden description method was not called. Can somebody explain what is happening here?
#interface MyArrayClassCluster : NSArray
#end
#implementation MyArrayClassCluster
{
NSArray *_realArray;
}
// Implement the class cluser stuff here
- (NSUInteger)count
{
return [_realArray count];
}
- (id)objectAtIndex:(NSUInteger)index
{
return [_realArray objectAtIndex:index];
}
// lifeCycle
- (id)initWithItems:(NSArray *)items
{
self = [super init];
_realArray = [items retain];
return self;
}
- (void)dealloc
{
[_realArray release];
[super dealloc];
}
- (NSString *)description
{
return [NSString stringWithFormat:#"My Custom Array: %p, objs:%#", self, _realArray];
}
#end
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSArray *a = #[#1, #2, #3];
NSLog(#"a: %#", a);
MyArrayClassCluster *clzCluster = [[MyArrayClassCluster alloc] initWithItems:a];
NSLog(#"clzCluster: %#", clzCluster);
}
return 0;
}
Output
2013-01-29 18:52:38.704 ClassClusterTester[31649:303] a: (
1,
2,
3
)
2013-01-29 18:52:38.707 ClassClusterTester[31649:303] clzCluster: (
1,
2,
3
)
The link #Rob pointed to had the correct answer at the bottom. An obscure fact: if something implements descriptionWithLocale:, NSLog will call that instead. Since my class is a subclass of NSArray, and NSArray implements that, my version of description was not called.

NSMutableArray object values not being retained when returning from my model .m file

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
// in .m file
static int *myInt; // only one `myInt` exist
// in .h or .m file
#interface MyClass : NSObject {
int myVar; // each MyClass instance will have its own `myVar` which are different
}
// somewhere else
MyClass *a = [[MyClass alloc] init];
MyClass *b = [[MyClass alloc] init];
a->myVar = 1;
b->myVar = 2;
// a->myVar is 1 and b->myVar is 2
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

Resources