0

我想要的是:

在 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 方法有关?

4

3 回答 3

3

carArray是弱属性,应该是强的。

另外:你应该使用self.carArray而不是carArray. 使用 时carArray,您使用的是基础 iVar,而不是实际属性。以后会造成混乱。

于 2012-07-24T12:30:20.623 回答
0

您的属性应该是,而不是property (weak, nonatomic), property (nonatomic, retain)

在您的 init 方法中,输入:

-(id)initWithProducer:(NSString *)varProducer 
             andModel:(NSString *)varModel 
             andPYear:(NSString *)varPYear 
             andPrice:(NSString *)varPrice
{
    self = [super init];
    if (self != nil) 
    {
        self.producer = varProducer;
        self.model = varModel;
        self.pYear = varPYear;
        self.price = varPYear;
    }
    return self;
}

然后,检查你carArray是否有对象,而不是 using if (carArray != nil), use if ([carArray count] > 0),因为你已经初始化了它。

于 2012-07-24T12:33:17.233 回答
0

你的 carArray 是弱属性。carArray 应该很强大。

于 2012-07-24T13:15:10.047 回答