Question

I'm pretty new to programming. I am creating a very simple iOS Quiz app just for practice. Here is what I have gotten done thus far:

  • I created the Xcode project using the "Single View" template. Thus, I already have the appDelegate files, the View Controller and a View (XIB file).

    • My view only has four controllers: 2 UILabels and 2 UIButtons. Each button is paired up with a label. I have all the connections for these 4 controllers setup. When the user taps the button labelled "Get a State" it needs to populate it's label with the name of a state I have in an NSMutableArray called stateArray. When the user taps on the button labelled "Get Capital" is needs to populate it's label with the state's capital in it's label.

    • I created an Objective-C class that inherits from NSObject to hold my data model called dataModel. In the dataModel.m file I have created and populated the two arrays.

    • In the view controller .m file I imported the dataModel.h file.

The only problem I am having is getting the view controller to retrieve data from the dataModel file. I have read that I should probably be using Delegation, but I am just looking to know how to do it more simply...I ready something about the view controller and the data model file should have references to each other? If so, what would the coding look like?

Here is my coding thus far:

#import <UIKit/UIKit.h>
@interface onMyOwnViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *stateField;
@property (weak, nonatomic) IBOutlet UILabel *answerField;

- (IBAction)answerButton:(id)sender;
- (IBAction)stateButton:(id)sender;
@end




#import "onMyOwnViewController.h"
#import "dataModel.h"

@implementation onMyOwnViewController

- (IBAction)stateButton:(id)sender
{

    NSString *myState = [stateArray objectAtIndex:0]; //this line produces an error.
    [_stateField setText:myState];
    [_answerField setText:@"hi"];    
}

- (IBAction)answerButton:(id)sender
{

}
@end

Below is my dataModel coding:

#import <Foundation/Foundation.h>

@interface dataModel : NSObject

@property(nonatomic, strong) NSMutableArray *answerArray;
@property(nonatomic, strong) NSMutableArray *stateArray;

@end



#import "dataModel.h"
#import "onMyOwnViewController.h"

@implementation dataModel

- (id)init
{
    self = [super init];
    if(self){
    _answerArray = [[NSMutableArray alloc]initWithObjects:@"Michigan", @"Illinios", nil];

    _stateArray = [[NSMutableArray alloc]initWithObjects:@"Lansing", @"Springfield",
                   nil];
    }

    return self;
}
@end

When I run the app, everything works except retrieving data from the data model. When you replay, please reply with coding.

Was it helpful?

Solution

You need to create an instance of your dataModel in onMyOwnViewController.

@implementation onMyOwnViewController {
  dataModel *data;
}

-(void)viewDidLoad {
  data = [[dataModel alloc] init];
}

Then in your method call

- (IBAction)stateButton:(id)sender
{

   NSString *myState = data.stateArray[0];
   ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top