0

I'm new to iOS development and I want to pass an NSMutableArray from one viewcontroller to another but always gives me null values

FirstViewController.h

@interface FirstViewController : UIViewController

@property (nonatomic, retain) NSMutableArray *colorArray;
-(IBAction)btn:(id)sender;

FirstViewController.m

@implementation FirstViewController


-(IBAction)btn:(id)sender
{


  SecondViewController* secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    secondViewController.animalArray = self.colorArray;

    NSLog(@"%@",secondViewController.animalArray); // here its not null

    [self.navigationController pushViewController:secondViewController animated:YES];

}

SecondViewController.h

@interface SecondViewController : UIViewController

@property (nonatomic, retain) NSMutableArray *animalArray;

SecondViewController.m

I only used NSLog(@"animalArray:%@",self.animalArray); in viewDidLoad to check the values but gives me null

is there anything I'm missing?

Edit :

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"indidLoad%@",self.animalArray);


}

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];

    NSLog(@"inwillAppear%@",self.animalArray);


 }
u_kami
  • 565
  • 12
  • 28
  • set the animalArray after you pushed the secondViewController. – Julien Klindt Jul 25 '13 at 10:12
  • Where do you create and set `colorArray`? – Wain Jul 25 '13 at 10:13
  • @JulienKlindt, how will that help? Particularly if the check is in `viewDidLoad`. – Wain Jul 25 '13 at 10:14
  • 2
    are you allocating and initializing animalArray in viewDidLoad? – Alex Jul 25 '13 at 10:16
  • The viewDidLoad of the second view controller is called immediately when you initialize it with a nib file, and at that moment you haven't yet passed the array from the first view controller. Thats why its nil when you log it in viewDidLoad – JonasG Jul 25 '13 at 10:17
  • @Wain yes you are right, viewDidLoad called before.. – Julien Klindt Jul 25 '13 at 10:18
  • Did u synthesize the object? – Lokesh Chowdary Jul 25 '13 at 10:18
  • @JonasG, are you sure. It should just be loaded when the view is first requested. – Wain Jul 25 '13 at 10:19
  • the colorArray is called in the viewDidLoad: self.colorArray= [dbAccess getColors]; where getColors returns an NSMutableArray of colors (I have no problem with this method it returns what I want) – u_kami Jul 25 '13 at 10:21
  • @Wain Yeah I'm pretty sure. The view controller is initialized with a nib file so it loads the view from the nib file when you init the view controller. I'm not 100% sure tho – JonasG Jul 25 '13 at 10:22
  • @Alex yes I tried but still nothing – u_kami Jul 25 '13 at 10:58
  • Create new array once again and check allocate and initialize color array in viewdidload of first view controller and in second view controller just property and synthesize animal array do not allocate and initialize animal array in second view controller – Alex Jul 25 '13 at 11:25
  • @Alex I tried but still same – u_kami Jul 25 '13 at 11:54
  • y u r using self .color array?/ – Alex Jul 25 '13 at 12:03
  • @Alex I tried without self but still same problem – u_kami Jul 25 '13 at 12:07
  • Most of the answers who answered here actually works..... u have to try with different methods.. finally declare @class secondviewcontroller in first and check instead of #import...otherwise create new NSArray and follow the steps what are all mention above it must come.. – Alex Jul 25 '13 at 12:28

6 Answers6

4

Replace with following method

-(IBAction)btn:(id)sender{
      SecondViewController* secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
         secondViewController.animalArray=[[NSMutableArray alloc]initWithArray:self.colorArray];
        [self.navigationController pushViewController:secondViewController animated:YES];
    }

:) +1

Mike
  • 737
  • 1
  • 6
  • 14
  • This is just nonsense, why would you pass a copy of the array to second view controller? – JonasG Jul 25 '13 at 10:24
  • still getting null value – u_kami Jul 25 '13 at 10:48
  • First check the self.colorArray contains the value and paste the viewdidload code of 2ndVC. :) – Mike Jul 25 '13 at 10:54
  • yes colorArray is not empty..also in the NSLog I wrote in the btn sender animalArray gives me the correct values but in the second view animalArray gives me null – u_kami Jul 25 '13 at 10:59
  • paste here your secondviewcontroler code(viewdidload,viewwillapear), it's work perfect there are some minor mistake in 2ndvc. – Mike Jul 25 '13 at 11:06
  • check my edited question, have you tried it and it works perfectly? – u_kami Jul 25 '13 at 11:33
  • 1
    REPLACE .h file -@interface SecondViewController : UIViewController{ NSMutableArray *animalArray;} -@property (nonatomic, retain) NSMutableArray *animalArray; AND IN .M FILE do the @synthesize animalArray; Don't forget +1 :) – Mike Jul 25 '13 at 11:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34152/discussion-between-userl-and-mindfreak) – u_kami Jul 25 '13 at 11:55
0

If you call your NSLog from (void)viewWillAppear:(BOOL) animated, you should see something.

In your code example the viewDidLoad method is called straight after initWithNibName :

[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

and before you have a chance to set your property.

Eric Genet
  • 1,260
  • 1
  • 9
  • 19
0
    **FirstViewController.h**

@interface FirstViewController : UIViewController
{
    NSMutableArray *SongArray;
}
@property(nonatomic,retain)NSMutableArray *SongArray;

**FirstViewController.m**

    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];    
    secondView.SongArray = self.SongArray;

    [self.navigationController secondView animated:YES];


    **SecondViewController.h**

    @interface SecondViewController : UIViewController
    {
        NSMutableArray *SongArray;
    }
    @property(nonatomic,retain)NSMutableArray *SongArray;

Do it like this your values arent being retained. Please also check Pass NSMutableArray to one view controller to another

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
0

Check the value in viewWillAppear instead of viewDidLoad.

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
  • I checked and still getting null value – u_kami Jul 25 '13 at 10:50
  • That is strange. I suggest adding breakpoints and checking every step with the debugger are you sure that assignment is made? Does the firstViewController have a value for SongArray? Is some other code assigning to SongArray? – Rob van der Veer Jul 25 '13 at 14:47
0

It should work

-(IBAction)btn:(id)sender
{


    SecondViewController* secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    NSLog(@"%@",secondViewController.animalArray); // here its not null

    [self.navigationController pushViewController:secondViewController animated:YES];
    secondViewController.animalArray = self.colorArray;

}

Sometimes if you pass value before pushing the the view it will give null value in second view

Krrish
  • 2,256
  • 18
  • 21
-2

Better way is to create your NSMutableArray in Appdelegate file like below...

@property (nonatomic, retain) NSMutableArray *animalArray;//define this in Appdelegate.h file

synthesize it in Appdelegate.m file like

@synthesize animalArray;

Create Appdelegate object in prefix.pch file like below.

#define AppDel ((AppDelegate *)[UIApplication sharedApplication].delegate)

and import your Appdelegate file in prefix.pch like below..

#import "AppDelegate.h"

now where you want to use that array...just write like below..

 AppDel.animalArray=//you can do whatever you want with array....

By doing this no need to pass array to other view controller, you can use that global array in whole project ,you can insert object in that array in any class and can access in any class.

Let me know it is working or not!!!

Happy coding!!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • it is because, by doing this , the push animation from one view controller to another will be smooth, just think that what if the array contains more than 500 values, it will take lone time to push..Just try it and then make my answer minus. – NiravPatel Jul 25 '13 at 10:21
  • Then you don't have to push it right away. Just make it async or load it from viewDidLoad or create a Singleton to handle your Data, but putting GlobalVars into the AppDelegate is the worst case. Just check discussion here: http://stackoverflow.com/questions/12046060/using-the-appdelegate-to-share-data – Julien Klindt Jul 25 '13 at 10:23
  • 1
    That is not the worst case my friend.It is the easiest way to maintain your array in whole project, can you tell me how many times you will decalre and alloc your array, if that array is in all view controllers.? – NiravPatel Jul 25 '13 at 10:25
  • AppDelegate is not a model. You should respect certain coding patterns. You could use a signleton model but never hold data in AppDelegate – JonasG Jul 25 '13 at 10:28
  • Your solution is possible one. Yes , you can persist the data in app delegate class . But is it clean way ? or in which cases we should do that is debatable point . Actually what I understood is , the app delegate should just handle the application-wide notifications and do nothing else. – V-Xtreme Jul 25 '13 at 10:33
  • If you define array in appdelegate class then , the class will contain only creation part, then you can insert objects in that array in any class except appdelegate,So there will no any burden on appdelegate class,i use it 100 times and my apps go smoothly. – NiravPatel Jul 25 '13 at 10:38
  • @NiravPatel. Using app delegate to pass values is against the standards, but yes many people including me uses it as a singleton object through out the app. What I did not understand is how you felt that by doing this the screen transition becomes smoother, according to my understanding unless u r not executing that in background both approach is gona take same time for any number of values in array. Please correct me if I am wrong. – Krrish Jul 25 '13 at 12:24