0

每次运行此方法时,都会创建并初始化我的数组。我怎样才能保持数组填充,直到

  • 移动到另一个视图或
  • 选择另一个按钮(我们称之为“重置”)。

    - (IBAction)addItemToOrder:(id)sender {
      NSMutableArray *arrayOrderItems = [[NSMutableArray alloc]init];
      NSMutableString *stringOrderItem = [[NSMutableString alloc]initWithString:@"Item: "];
      [stringOrderItem appendString:self.labelProductCode.text];
      [stringOrderItem appendString:@" | Qty: "];
      [stringOrderItem appendString:self.textfieldQuantity.text];
      NSObject *myObj = [[NSObject alloc]init];
      myObj = stringOrderItem;
      [arrayOrderItems addObject:myObj];
      self.textviewOrderTotal.text = stringOrderItem;
      NSLog(@"arrayOrderItems: #records: %d", arrayOrderItems.count);
      NSLog(@"%@", arrayOrderItems);
    }
    
4

1 回答 1

0

您应该使用数组作为实例变量

// MyClass.h
@interface MyClass : NSObject
    @property (nonatomic, strong) NSMutableArray *myArray;
@end

// MyClass.m
@implementation MyClass

- (id)init
{
    self = [super init];
    if (nil != self)
    {
        self.myArray = [NSMutableArray array];
    }
    return self;
}

- (IBAction)addItemToOrder:(id)aSender
{
    [self.myArray addObject:@"MyOrder"];
    NSLog(@"%@",testArray.description); 
}

@end
于 2013-09-16T15:26:07.327 回答