4

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!

Sequenzia
  • 2,193
  • 8
  • 34
  • 57

4 Answers4

6

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

-(NSString *)getFoo;
graver
  • 14,963
  • 4
  • 44
  • 62
  • 2
    There is something more sinister going on here though; the OP doesn't seem to understand that you shouldn't just create instances of a view controller, which is not connected to the view hierarchy, in order to just call a single method. – trojanfoe Feb 27 '13 at 07:42
  • 1
    Agreed. Instance vars should be used and the viewDidLoad method shouldn't be where a view controller is initialized. – erran Feb 27 '13 at 07:44
  • I am giving answer to the question being asked. If I have to surround my question with naming conventions, MVC practices, correct `UIViewController` implementations, etc., I would suggest staring over the whole iOS development. – graver Feb 27 '13 at 07:47
  • That did it. Thanks. This was obviously just a test and not real code. I am just trying to familiarize myself with objective-c classes. Thanks for the help – Sequenzia Feb 27 '13 at 07:48
  • 1
    @graver And you have no comment to make about this glaring mistake? – trojanfoe Feb 27 '13 at 07:50
  • 1
    @trojanfoe I have, but it's obvious that @Sequenzia is just getting familiar with classes, methods, etc. and there's a long way to `UIViewControllers`, their implementation, hierarchy, interactions... – graver Feb 27 '13 at 07:51
1

Did you put - (NSString *)getFoo in your geoMainViewController.h ? You have to make those methods visible to the outside of your object through the .h file, so other objects know which selectors they respond to. Did the autoComplete fill in the message per chance?

#import <Foundation/Foundation.h>

@interface 
{

}

@property (nonatomic,strong) ;
@property (nonatomic,strong) ;
@property (nonatomic, strong) ;

- (NSString *)getFoo
@end

EDIT: (You could also just make Foo a property by the way)

blaa
  • 430
  • 5
  • 19
1

Did you declare it in your header file?

Header file contains all the function declarations in the .h file and you only include the .h file in your class. So it depends on .h file. .h file will have all the functions as the .m file.

Hope it helps you.

lakesh
  • 25,623
  • 57
  • 163
  • 254
0

You are misunderstanding how to use a view controller. While you can technically create an instance of a view controller in order to call one of its methods, you shouldn't do so. The normal approach is that the view controller is part of the view hierarchy and you can call methods on it when you have access to that instance. You are missing something fundamental here.

Your actual error is a missinh method declaration, I would suspect, but you have bigger problems to solve first.

trojanfoe
  • 116,099
  • 18
  • 197
  • 233
  • Thanks for the feedback. I understand what you are saying but I have a function that is exactly the same and I need to be able to call it from 2 different view controllers. I am trying to avoid duplicate code. Would there be a better place to put the function. Maybe the app delegate? My code is working now but I want to make sure I am following best practices. Thanks! – Sequenzia Feb 27 '13 at 07:53
  • @Sequenzia It depends what the method does. – trojanfoe Feb 27 '13 at 08:47