Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan

iOS Programming => General Questions => : lozjoyk September 06, 2011, 10:32:34 AM



: Please could anyone help me passing an array from one class to another
: lozjoyk September 06, 2011, 10:32:34 AM
Hi, i have a standard tab bar application and i am trying to pass an array from the first view to the second view but really struggling.

here is my FirstViewController.h:
: (Objective-C)
#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {

    NSArray *theArray;
   
}

@property (nonatomic, retain) NSArray *theArray;

@end

FirstViewController.m:
: (Objective-C)
#import "FirstViewController.h"

@implementation FirstViewController
@synthesize theArray;

- (void)viewDidLoad {
   
    [super viewDidLoad];   
   
    theArray = [NSArray arrayWithObjects:@"A",@"B", @"C",nil];
   
    NSLog(@"theArray - %@", theArray);

}
...

its in the second view that i want to retrieve the values of the array.

SecondViewController.h:
: (Objective-C)
#import <UIKit/UIKit.h>
#import "FirstViewController.h"

@interface SecondViewController : UIViewController {
   
}

@end

SecondViewController.m:
: (Objective-C)
#import "SecondViewController.h"

@implementation SecondViewController


- (void)viewDidLoad {
   
    [super viewDidLoad];
   
    FirstViewController *firstViewArray = [[FirstViewController alloc] init];
   
    NSArray *arrayValues = [firstViewArray theArray];
   
    NSLog(@"second view array - %@", arrayValues);
   
   
}
...

the result i get from NSLog is ...

second view array - (null)

and i cant figure out why it wont pass across.

any help would be greatly appreciated.


: Re: Please could anyone help me passing an array from one class to another
: fujilla September 06, 2011, 12:16:10 PM
With
FirstViewController *firstViewArray = [[FirstViewController alloc] init]; 
what you are actually saying is create a new instance of FirstViewController called firstViewArray, not actually creating an array called firstViewArray.

What are you wanting to do with the contents of the array?

Nick
http://myfirstiphoneapp.co.uk (http://myfirstiphoneapp.co.uk)


: Re: Please could anyone help me passing an array from one class to another
: fujilla September 07, 2011, 02:10:15 AM
Hopefully one or some of these will help you:

http://www.devx.com/wireless/Article/42476/1954 (http://www.devx.com/wireless/Article/42476/1954)
http://forums.pragprog.com/forums/83/topics/3604 (http://forums.pragprog.com/forums/83/topics/3604)
http://gregorykaiser.wordpress.com/2009/05/13/385/ (http://gregorykaiser.wordpress.com/2009/05/13/385/)
http://forums.bignerdranch.com/viewtopic.php?f=87&t=2828 (http://forums.bignerdranch.com/viewtopic.php?f=87&t=2828)
http://forums.bignerdranch.com/viewtopic.php?f=49&t=487 (http://forums.bignerdranch.com/viewtopic.php?f=49&t=487)
http://www.iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-other-objects.html (http://www.iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-other-objects.html)
http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers (http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers)

Nick
http://myfirstiphoneapp.co.uk/ (http://myfirstiphoneapp.co.uk/)


: Re: Please could anyone help me passing an array from one class to another
: lozjoyk September 09, 2011, 02:49:56 AM
Hi, thanks for the reply.

I got round it by setting the arrays to global, is this a bad way? i only want to make reference to the array in other views.

Cheers.


: Re: Please could anyone help me passing an array from one class to another
: fujilla September 09, 2011, 03:09:40 AM
I guess if it is a global/constant which will never change, then it is probably OK.

Nick


: Re: Please could anyone help me passing an array from one class to another
: fujilla September 09, 2011, 06:43:21 AM
Here is a link to my latest blog post, as the subject of passing data between view controllers is something I had initially struggled to do with my first app:

http://myfirstiphoneapp.co.uk/2011/09/passing-data-between-view-controllers-using-a-singleton-class/ (http://myfirstiphoneapp.co.uk/2011/09/passing-data-between-view-controllers-using-a-singleton-class/)


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.