Mohammed
Mohammed

Reputation: 97

How to pass array form one view to another view in iphone

I am new to i phone programming.I have store some data in array that i want to pass that array data form one view to another view.What i am tried but its not working.Can any body tell me what is mistake in that.

#import"firstviewcontroller.h"

 @property(nonatomic,retain)NSMutableArray *tapCollection;
 @property(nonatomic,retain)NSMutableArray *imageCollection;

#import"firstviewcontroller.m"

   @synthesize imageCollection,tapCollection;
    -(void)viewdidload
    {
    self.tapCollection = [[NSMutableArray alloc] init];
    self.imageCollection = [[NSMutableArray alloc] init];
    }
    - (void)insertNewObject:(id)sender
    {

        secondviewcontroller*r= [[secondviewcontrolleralloc] initWithNibName:@"secondviewcontroller" bundle:nil];
        self.navigationController.navigationBar.tintColor=[UIColor blackColor];
         r.imageCollection1 =imageCollection;
             r.tapCollection1 =tapCollection;
        [self.navigationController pushViewController:r animated:YES];

    }

Acctually what data i store in array is images and button tag values.Here in console its displaying Images and button tag value i have store in array

2013-03-19 21:54:03.374 Taukyy[290:1c103] (
    0,
    "<UIImage: 0x9cd59e0>",
    1,
    "<UIImage: 0x9cd6220>",
    2,
    "<UIImage: 0x9cd6b70>"
)

import"secondviewcontroller.h"

@property(nonatomic,retain)NSMutableArray *tapCollection1;
@property(nonatomic,retain)NSMutableArray *imageCollection1;

import"secondviewcontroller.m"

@synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
    imageCollection1 = [[NSMutableArray alloc] init];
   tapCollection1 = [[NSMutableArray alloc] init];
   NSLog(@"%@",tapCollection1);
   NSLog(@"%@",imageCollection1);
}

But here values are not displaying.Its showing as below

2013-03-19 21:29:16.379 Taukyy[594:1c103] (
)
2013-03-19 21:29:16.380 Taukyy[594:1c103] (
)
2013-03-19 21:29:16.381 Taukyy[594:1c103] (
)

Please can any body tell me what is mistake in this code Thanks Aslam

Upvotes: 4

Views: 1934

Answers (4)

Subbu
Subbu

Reputation: 2101

Do not init the mutableArrays in viewDidLoad of SecondViewController , instead do it in the init method of SecondViewController

 -(id)initWithNibName //this method
  {
     //create object

      self.imageCollection1 = [NSMutableArray alloc] init];
      self.tapCollection1 = [NSMutableArray alloc] init];

   }

You will get the right values now..

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31081

Why are you allocating previously allocated array.

just remove

imageCollection1 = [[NSMutableArray alloc] init];
tapCollection1 = [[NSMutableArray alloc] init];

Upvotes: 0

rog
rog

Reputation: 5361

Don't allocate/initialize your arrays again. You set them when you're preparing your segue.

@synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
   //imageCollection1 = [[NSMutableArray alloc] init];
   //tapCollection1 = [[NSMutableArray alloc] init];
   NSLog(@"%@",tapCollection1);
   NSLog(@"%@",imageCollection1);
}

Upvotes: 3

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

Remove the viewDidLoad's array allocations from secondviewcontroller.m

You tapCollection1 & imageCollection1 are retain properties.So It should retain the assigned objects.

Your secondviewcontroller.h should look like,

@property(nonatomic,retain)NSMutableArray *tapCollection1;
@property(nonatomic,retain)NSMutableArray *imageCollection1;

and

- (void)viewDidLoad
{
  //remove the allocation codes
}

You can log it in firstviewcontroller.m like,

 - (void)insertNewObject:(id)sender
    {

         secondviewcontroller*r= [[secondviewcontrolleralloc] initWithNibName:@"secondviewcontroller" bundle:nil];
         self.navigationController.navigationBar.tintColor=[UIColor blackColor];
         r.imageCollection1 =imageCollection;
         r.tapCollection1 =tapCollection;
         NSLog(@"%@",r.imageCollection1);
         NSLog(@"%@",r.tapCollection1);
        [self.navigationController pushViewController:r animated:YES];

    }

Upvotes: 3

Related Questions