Welcome, Guest. Please login or register.
Did you miss your activation email?
July 27, 2016, 12:43:16 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ iOS Programming
| |-+ General Questions
| | |-+ Please could anyone help me passing an array from one class to another
Pages: [1] Go Down
Print
Author Topic: Please could anyone help me passing an array from one class to another (Read 5151 times)
lozjoyk
Newbie
*
Posts: 8


on: 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:
Code: (Objective-C)
#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {

    NSArray *theArray;
   
}

@property (nonatomic, retain) NSArray *theArray;

@end

FirstViewController.m:
Code: (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:
Code: (Objective-C)
#import <UIKit/UIKit.h>
#import "FirstViewController.h"

@interface SecondViewController : UIViewController {
   
}

@end

SecondViewController.m:
Code: (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.
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW
Reply #1 on: 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
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW
Reply #2 on: 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://forums.pragprog.com/forums/83/topics/3604
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=49&t=487
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

Nick
http://myfirstiphoneapp.co.uk/
Logged
lozjoyk
Newbie
*
Posts: 8


Reply #3 on: 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.
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW
Reply #4 on: 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
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW
Reply #5 on: 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/
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.