views:

39

answers:

1

I have an array initialized in my RootViewController and a method that addsObjects to an array. I created a RootViewController object in my SecondViewController. The method runs (outputs a message) but it doesn't add anything to the array, and the array seems empty. Code is below, any suggestions?

RootViewController.h

#import "RootViewController.h"
#import "SecondViewController.h"

@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    myArray2 = [[NSMutableArray alloc] init];

    NSLog(@"View was loaded");
}
-(void)addToArray2{
    NSLog(@"Array triggered from SecondViewController");
    [myArray2 addObject:@"Test"];
    [self showArray2];
}

-(void)showArray2{
    NSLog(@"Array Count: %d", [myArray2 count]);
}
-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

SecondViewController.m

#import "SecondViewController.h"
#import "RootViewController.h"

@implementation SecondViewController

-(IBAction)addToArray{

    RootViewController *object = [[RootViewController alloc] init];
    [object addToArray2];

}
-(IBAction)switchBack{
    [self dismissModalViewControllerAnimated:YES];
}

EDIT*****

With Matt's code I got the following error:

" expected specifier-qualifier-list before 'RootViewController' "

A: 

You are missing some very essential fundamentals here. If you allocate a new RootViewController in your SecondViewController, it is not the same instance as the one you used to create your SecondViewController so it will not have a reference to the array you are adding objects to. What you are trying to do won't work. You must create an ivar in your SecondViewController for your RootViewController and then access it inside second view. Something like this:

-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] 
                                       initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [screen setRootViewController:self];
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

Your ivar would need declared like this in the SecondViewController.h:

@property (nonatomic, retain) RootViewController *rootViewController;

And then synthesized in the .m

Then, you can access the ivar from within your SecondViewController:

-(IBAction)addToArray{
    [[self rootViewController] addToArray2];
}
Matt Long
Let me ask you. The [screen setRootViewController:self] does what? From what I think, this line of code almost makes the RootViewController the native controller when the SecondViewController nib is loaded?
Antonio
It simply gives your SecondViewController a reference to the RootViewController so that it can access it and call it's -addToArray2 method that you created. I'm not sure what you mean by *native* controller.
Matt Long
Never mind you cleared my confusion :) I will try this. Thank you for the explanation.
Antonio
Matt: I tried doing what you said and I got the following error: error: expected specifier-qualifier-list before 'RootViewController'
Antonio
Matt Long
Thank you for the book link :)
Antonio