我想要的是:
在 iPhone 上,用户输入几个值并点击保存按钮。应用程序应将值存储在 的实例中VehicleClass
并将其添加到NSMutableArray
.
希望你能帮忙。
视图控制器.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
视图控制器.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.";
}
}
车辆类.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
车辆类.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
也许问题与我的 init 方法有关?