我想在我的方法中添加我的数组,而不是每次调用方法时都重置它

时间:2013-09-16 11:00:14

标签: ios objective-c arrays

每次运行此方法时,都会创建并初始化我的数组。如何保持阵列填充

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

    - (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);
    }
    

1 个答案:

答案 0 :(得分:0)

您应该将数组用作instance variable

// 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