jrkolsby
jrkolsby

Reputation: 51

Externally accessing a variable from ViewController

I am trying to access an array created in ViewController.m from AppDelegate.m using a method on ViewController. When I try to send the message in AppDelegate.m, XCode gives me the error,

"No known class method for selector ..."

In ViewController.h:

-(NSMutableArray *)getButtonArray;

In ViewController.m:

- (NSMutableArray *)getButtonArray;
{
    NSMutableArray *buttonArray = [[NSMutableArray alloc] init];

    for (ElementButton *button in [self.view subviews]) {
        [buttonArray addObject:button];
    }
    return buttonArray;
}

In AppDelegate.m:

NSMutableArray *buttonArray = [ViewController getButtonArray];

I am not understanding why I cannot call this method on ViewController, as I have declared it in its class files. If this is something that is not allowed for some reason, is there another way to achieve this same effect?

Upvotes: 0

Views: 94

Answers (1)

Apurv
Apurv

Reputation: 17186

getButtonArray is an instance method. So, you need to create the instance for it.

ViewController *controller = [[ViewController alloc] init];
NSMutableArray *buttonArray = [controller getButtonArray];

Upvotes: 2

Related Questions