What I want:
On the iPhone user enters few values and tap on save button.
App should store the values in an instance of VehicleClass and add it to an NSMutableArray.
Hope you can help.
ViewController.h
#import <UIKit/UIKit.h>
#import "VehicleClass.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *producerTextField;
@property (weak, nonatomic) IBOutlet UITextField *modelTextField;
@property (weak, nonatomic) IBOutlet UITextField *pYearTextField;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@property (weak, nonatomic) NSMutableArray *carArray;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
- (IBAction)addCar:(id)sender;
- (IBAction)showCars:(id)sender;
@end
ViewController.m
@synthesize producerTextField, modelTextField, pYearTextField, priceTextField, carArray;
- (void)viewDidLoad
{
[super viewDidLoad];
carArray = [NSMutableArray array];
}
- (IBAction)addCar:(id)sender
{
VehicleClass *newVehic = [[VehicleClass alloc]initWithProducer:producerTextField.text
andModel:modelTextField.text
andPYear:producerTextField.text
andPrice:priceTextField.text];
[carArray addObject:newVehic];
if (carArray != nil) {
statusLabel.text = @"Car saved to array.";
}
else {
statusLabel.text = @"Error, check your code.";
}
}
VehicleClass.h
#import <Foundation/Foundation.h>
@interface VehicleClass : NSObject
@property (weak, nonatomic) NSString *producer;
@property (weak, nonatomic) NSString *model;
@property (weak, nonatomic) NSString *pYear;
@property (weak, nonatomic) NSString *price;
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice;
@end
VehicleClass.m
#import "VehicleClass.h"
@implementation VehicleClass
@synthesize model, price,pYear,producer;
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice
{
self = [super init];
if (self != nil)
{
producer = varProducer;
model = varModel;
pYear = varPYear;
price = varPYear;
}
return self;
}
@end
Maybe the problem has something to do with my init method?
carArrayis a weak property, it should be strong.Also: you should use
self.carArrayand notcarArray. When you usecarArray, you are using the underlying iVar and not the actual property. It will cause confusion later.