Daniel Medina Sada
Daniel Medina Sada

Reputation: 478

Using mutable arrays in other classes

I've tried several ways that i've found here but none have worked. what would be an easy way to pass this NSMutalbeArray into another View controller?

    NSMutableArray *pph = [[NSMutableArray alloc] init];
    [pph addObject:[NSString stringWithFormat:@"%d     %d     %d",diario.Cowid,diario.Lact,diario.Del]];

on the same file below i have

- (IBAction)masInfoPPH;
{
    tipo = @"PPH";
    adiario = pph;
    NSLog(@"\n Array adiario: %@",pph);
    DetDiarioViewController *DetDiarios = [[DetDiarioViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:DetDiarios animated:YES];
}

for some reason pph (the NSMutalbeArray) gets here as null but up there it does give me the objects it should have. adiario is a global array or at least its supposed to be. Help!

Upvotes: 0

Views: 77

Answers (2)

Alex Gray
Alex Gray

Reputation: 16463

The scope of your pph array is unclear from your description.. But ANYTHING declared inside a single method is LOCAL to that method, unless it is returned BY that method.

You have several options... Declare the array as an instance variable, ie..

@interface YourClass: NSObject {  NSMutableArray *pph; }

or

@implementation YourClass {  NSMutableArray *pph; }

or as a static variable (in your .m file) (which would enable you to access the value from Class (+) methods..

static NSMutableArray *pph = nil;

or most preferably... as a property

@interface YourClass @property (strong) NSMutableArray *pph;

which you can then call upon from any instance method via the automatically synthesized Ivar _pph, or via the property's accessors.. self.pph.

Hope this helps!

Upvotes: 0

KHansenSF
KHansenSF

Reputation: 604

There really are no global arrays. Create a property in your class for pph in the interface of your class.

@property(nonatomic, strong) NSMutableArray *pph;  



self.pph = [[NSMutableArray alloc] init];
[self.pph addObject:[NSString stringWithFormat:@"%d     %d     %d",diario.Cowid,diario.Lact,diario.Del]]

But you still need to get that into next view controller. Create a similar property in it's interface and then set it before pushing

DetDiarioViewController *detDiarios = [[DetDiarioViewController alloc] initWithNibName:nil bundle:nil];
detDiarios.pph = self.pph;
[self.navigationController pushViewController:detDiarios animated:YES];

BTW - in objective-c the convention is to use a lowercase letter for the first letter of an instance

Upvotes: 1

Related Questions