Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Call method from another class

I have 2 classes geoViewController and geoMainViewController

I have a method in the geoMainViewController called getFoo

It looks like this:

- (NSString *)getFoo
{

NSString* foo = @"This is foo";

return foo;

}

I am trying to call getFoo from the geoViewController class.

I have #import "geoMainViewController.h" in my geoViewController m file.

I am trying instantiate the geoMainViewController class and call the getFoo method from the viewDidLoad in my geoViewController class like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    geoMainViewController* mainVC = [[geoMainViewController alloc] init];

    NSString* myFoo = [mainVC getFoo];    

}

It seems to be instantiating the geoMainViewController class fine but I am getting an error on NSString* myFoo = [mainVC getFoo];

The error is - no visible @interface for 'geoMainViewController' declares the selector 'getFoo'

I am sure I am missing a step because I am very new to Objective C. I am just not sure what I am doing wrong.

Any help on this would be great.

Thanks!

like image 611
Sequenzia Avatar asked Feb 18 '23 09:02

Sequenzia


1 Answers

In your geoMainViewController.h you should declare the selector to be visible:

-(NSString *)getFoo;
like image 93
graver Avatar answered Feb 26 '23 20:02

graver