2个按钮动作中1个类中的可变数组

时间:2014-10-25 03:40:06

标签: ios objective-c

我通过1个IBAction生成一些不可变数组但是当我从同一个类中的另一个IBAction引用它时,我无法检索答案。我在第一个动作中创建的部分代码是:

所有数组都是在第一个IBAction内的init。以下是vType的示例:

NSMutableArray *vType = [[NSMutableArray alloc] init];

// Gather components for Value Calculations  

//NSLog(@" myInv count(h) = %lu and catDetail count(g) = %lu", (unsigned long)[myInv count], (unsigned long)[catDetail count]);

for (h = 0; h < [myInv count]; h++){

    //NSLog(@"vScott objectAtIndex:h = %@ AT %d", [vScott objectAtIndex:h], h);

    for (g = 0; g < [catDetail count]; g++){

        //NSLog(@"cvScott objectAtIndex:g = %@ AT %d", [cvScott objectAtIndex:g], g);

        //NSLog(@"vScott objectAtIndex:h = %@ AT %d", [vScott objectAtIndex:h], h);

        if ([[vScott objectAtIndex:h] isEqualToString:[cvScott objectAtIndex:g]]){

            [vSerStart insertObject:[cvSerStart objectAtIndex:g] atIndex:h];
            [vType insertObject:[cvType objectAtIndex:g] atIndex:h];
            [vUnitCost insertObject:[cvFacePrice objectAtIndex:g] atIndex:h];
            [vYear insertObject:[cvYear objectAtIndex:g] atIndex:h];

            //NSLog(@"vScott (vIndex) / cvScott (cvIndex) = %@ (%d) / %@ (%d)", [vScott objectAtIndex:h], h,[cvScott objectAtIndex:g],g);
            //NSLog(@"cvSerStart Object %@ atIndex:p = %d",[cvSerStart objectAtIndex:g], g);
            //NSLog(@"cvType Object %@ atIndex:p = %d",[cvType objectAtIndex:g], g);
            //NSLog(@"vScott / vSerStart / vType // AT (myInv) Index p =  %@ / %@ / %@ // %d", [vScott objectAtIndex:h],[vSerStart objectAtIndex:h], [vType objectAtIndex:h],h);
            //NSLog(@"+++++++++++++++++++++++++++++++++++++++++");


            //NSLog(@" The value calculation Scott Numbers are:%@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@",[vScott objectAtIndex:h], [vExt objectAtIndex:h], [vSer objectAtIndex:h], [vNew objectAtIndex:h], [vUsed objectAtIndex:h], [vPlate objectAtIndex:h], [vSht objectAtIndex:h], [vSerPrice objectAtIndex:h], [vNewPrice objectAtIndex:h], [vUsedPrice objectAtIndex:h], [vPBPrice objectAtIndex:h], [vShtPrice objectAtIndex:h], [vSerStart objectAtIndex:h],[vType objectAtIndex:h]);

            vOutLine = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@", [vScott objectAtIndex:h], [vYear objectAtIndex:h], [vExt objectAtIndex:h], [vType objectAtIndex:h], [vSer objectAtIndex:h],[vNew objectAtIndex:h], [vUsed objectAtIndex:h], [vPlate objectAtIndex:h], [vSht objectAtIndex:h], [vUnitCost objectAtIndex:h], [vSerPrice objectAtIndex:h], [vNewPrice objectAtIndex:h], [vUsedPrice objectAtIndex:h], [vPBPrice objectAtIndex:h], [vShtPrice objectAtIndex:h], [vSerStart objectAtIndex:h]];

    //NSLog(@"vOutLine = %@", vOutLine);
    NSLog(@"%@",[vType objectAtIndex:h]);

    [valueData addObject:vOutLine];

Mutable Array(vType)的结果是:

2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C
2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C
2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C
2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C

尝试使用我在使用此代码测试的同一个类中的第二个Action中存储的值:

- (IBAction)btnValueCollectionDataTable:(id)sender {

    int k,l,m,n;
    NSMutableArray *vType;

    //if ([[vScott objectAtIndex:h] isEqualToString:[cvScott objectAtIndex:g]]

    for (k = 0; k < [myInv count]; k++){

        NSLog(@"%@",[vType objectAtIndex:k]);
        //if ([[vType objectAtIndex:k] isEqualToString:@"C"]){

           //convert required elements to Integer


           //calculate required elements

    //}else{

    }
}

vType阵列的此试验结果如下:

2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)
2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)
2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)
2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)

我认为一旦在类中初始化,阵列就可以在任何地方使用。我很感激建议,因为我被困在这里。

2 个答案:

答案 0 :(得分:1)

只需将NSMutableArrays声明为每个方法的本地。如果要从多个方法访问相同的NSMutableArray,请将其作为类(或实例变量)的属性。

您需要阅读变量范围的基础知识。

答案 1 :(得分:1)

vType数组是第一个IBAction的本地数组。因此,其范围仅限于第一次行动。 如果要在第二个IBAction中访问vType数组,请将其声明为接口中的属性。

@property(nonatomaic,strong) NSMutableArray * vTypeArray;

在第一和第二次IBAction访问此属性。