-2

I'm trying to access a property from view controller one, so I put the code in ViewDidLoad of SecondViewController:

 SeriesViewController *teste = [[SeriesViewController alloc] init];
 NSLog(@"%@", teste.seriesArray);

The array is null. The declaration of the property in VC1 is:

@property (strong, nonatomic) NSArray *seriesArray;

I'm calling second VC in method didSelectRowsAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *exercicios = [sb instantiateViewControllerWithIdentifier:@"ExerciciosViewController"];

    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    if ([[_seriesForDisplay objectAtIndex:indexPath.row] isEqualToString:@"Serie A"]) {
        [exercicios setTitle:[_seriesForDisplay objectAtIndex:indexPath.row]];
        NSLog(@"AGORA %@", array.exerciciosArray);

I also tried the opposite way, like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *exercicios = [sb instantiateViewControllerWithIdentifier:@"ExerciciosViewController"];

    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    if ([[_seriesForDisplay objectAtIndex:indexPath.row] isEqualToString:@"Serie A"]) {
        [exercicios setTitle:[_seriesForDisplay objectAtIndex:indexPath.row]];
        ExerciciosViewController *array = [[ExerciciosViewController alloc] init];
        array.exerciciosArray = _seriesArray;
        NSLog(@"AGORA %@", array.exerciciosArray);

But the exerciciosArray, although is populated in this code at VC1, is empty when I get to VC2.

Thanks.

UPDATE

I was instantiating a new controller. Now, I'm just pointing to existing one. I'm getting a different error now at:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section, I have the lines: 

NSLog(@"CONTADOR bla %li", _exerciciosArray.count); and: 

return _exerciciosArray.count; 

I get 4 items in the log just before crash with message:

-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'.
Unheilig
  • 16,196
  • 193
  • 68
  • 98
ferrojr
  • 339
  • 2
  • 4
  • 9

2 Answers2

0

You are likely not populating the array after the view controller is initialized, but before you reference it. If you are populating your array in the viewDidLoad, for example, then this could be your problem.

If you populate your array in the init method, you should be fine. So, you could do something like this:

@implementation SeriesViewController 
-(id)init {
    self = [super init];
    if (self!=nil)
    {
         [self populateSeriesArray];
    }
    return self;
}
-(void)populateSeriesArray
{
     NSArray *arr = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", nil];
     self.seriesArray = arr;
}
@end
Jeremy
  • 8,902
  • 2
  • 36
  • 44
0

You're creating a new instance of UIViewController instead of pointing to the existing/instantiated one. That's probably why the Array is empty.

You could use protocol/delegate, like explained here: How do I set up a simple delegate to communicate between two view controllers?

Or store the array, for example in NSUserDefaults. Although NSUserDefaults is not really meant for this kind of storage, it works fine.

Community
  • 1
  • 1
Mathijs
  • 1,258
  • 1
  • 10
  • 27
  • Your answer is correct. I was instantiating a new controller. Now, I'm just pointing to existing one. I'm getting a different error now at `- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section`, I have the lines `NSLog(@"CONTADOR bla %li", _exerciciosArray.count);` and `return _exerciciosArray.count;` I get 4 items in the log just before crash with message -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'. Thanks a lot for your help. – ferrojr Dec 16 '13 at 19:34
  • Maybe better bot to synthesize? Read this http://stackoverflow.com/questions/9090764/what-does-synthesize-variable-variable-really-do-is-it-very-helpful – Mathijs Dec 16 '13 at 19:51
  • No, it doesn't really change anything. Same crash. Thanks – ferrojr Dec 16 '13 at 19:55
  • You're saying that the log is returning 4 items but is it returning 4 items where an actual count of the array is displayed an it's higher than 0? You're maybe still pointing to an empty array still. Other possibility: is the crash caused in the numberOfSections method? – Mathijs Dec 16 '13 at 20:02
  • no, the crash is in numberofrowsinsection, when I comment the line `return _exerciciosArray.count;`it works, but my tableview only shows two cells. , NSLog, array.count returns 4. thanks – ferrojr Dec 16 '13 at 20:20