Search code examples
objective-ciosxcode4

Declaring an Array in my Model to use in the Control


I'm new to Objective-C and MVC. Ive been following along with Paul Haggerty's Coursework and lectures and learned a lot. I am entering a phase in my programming that I am actually able to sit down and code working apps rather than solely reading about iOS development.

I'm having difficulty understanding the how to use MVC properly.

Here is the (VERY BASIC) code that I've written and got to "work":

- (IBAction)buttonClicked {
    NSArray *namesArray = [NSArray arrayWithObjects:
                           (NSString *)@"Tiffany",
                           (NSString *)@"Jason",
                           (NSString *)@"Mustafa",
                           (NSString *)@"Mellisa",
                           (NSString *)@"Michael",
                           (NSString *)@"Kasim",
                           nil];

    if ([self.myDisplay.text
         isEqualToString:[namesArray objectAtIndex:0]]){
        self.myDisplay.text = [namesArray objectAtIndex:1];
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:1]]){
        self.myDisplay.text = [namesArray objectAtIndex:2];
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:2]]){
        self.myDisplay.text = [namesArray objectAtIndex:3];
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:3]]){
        self.myDisplay.text = [namesArray objectAtIndex:4]; 
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:4]]){
        self.myDisplay.text = [namesArray objectAtIndex:5];
    } else {
        self.myDisplay.text = [namesArray objectAtIndex:0];
    }

    self.numberOfLetters.text = [NSString stringWithFormat:@"%@ Letters", [NSString stringWithFormat:@"%d", self.myDisplay.text.length - 1]];
 }

As you can see it sets up an array and then when the user clicks the button on screen it displays the next name and the number of letters in that name. What I want to do is create my Array in my model and then access it through the controller (that's what you're supposed to do right?)

My approach to it was to create a model class (which i called Brain). I imported the brain into the controller and in the brain I created an NSArray property and synthesized it. But I'm having difficulty accessing it in the controller.

Also, I know that the way I have it done now is wrong because I'm basically recreating the array every time the user clicks the button.

Can someone please guide me? (I'm using ARC, btw.)

Here is how I created my Brain" class:

#import <Foundation/Foundation.h>

@interface Brain : NSObject
@property (nonatomic, strong) NSMutableArray *myNamesArray;

@end

#import "Brain.h"

@implementation Brain
@synthesize myNamesArray = _myNamesArray;

@end

Solution

  • I would just keep track of the index. Define it as a property in your class header. Also, you probably don't want to keep creating new arrays.

    @property (nonatomic) int index;
    @property (nonatomic, retain) NSArray *namesArray;
    

    Synthesize it in your implementation:

    @synthesize index, namesArray;
    

    Create the array in your init method (or viewDidLoad method if you prefer):

    self.namesArray = [NSArray arrayWithObjects:
                           @"Tiffany",
                           @"Jason",
                           @"Mustafa",
                           @"Mellisa",
                           @"Michael",
                           @"Kasim",
                           nil];
    

    If you're not using ARC, be sure to release namesArray in the dealloc method.

    Then just use that to set the text string.

    - (IBAction)buttonClicked {
        self.index++;
        self.myDisplay.text = [self.namesArray objectAtIndex:index%[namesArray count]];
    }