每次运行此方法时,都会创建并初始化我的数组。如何保持阵列填充
选择另一个按钮(让我们称之为“重置”)。
- (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);
}
答案 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