简体   繁体   English

Objective C - 来自另一个类的Call方法

[英]Objective C - Call method from another class

I have 2 classes geoViewController and geoMainViewController 我有2个类geoViewControllergeoMainViewController

I have a method in the geoMainViewController called getFoo 我在geoMainViewController有一个名为getFoo

It looks like this: 它看起来像这样:

- (NSString *)getFoo
{

NSString* foo = @"This is foo";

return foo;

}

I am trying to call getFoo from the geoViewController class. 我试图从geoViewController类调用getFoo

I have #import "geoMainViewController.h" in my geoViewController m file. 我的geoViewController m文件中有#import "geoMainViewController.h"

I am trying instantiate the geoMainViewController class and call the getFoo method from the viewDidLoad in my geoViewController class like this: 我正在尝试实例化geoMainViewController类并从我的geoViewController类中的viewDidLoad调用getFoo方法,如下所示:

- (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]; 它似乎是在实例化geoMainViewController类,但我在NSString* myFoo = [mainVC getFoo];上收到错误NSString* myFoo = [mainVC getFoo];

The error is - no visible @interface for 'geoMainViewController' declares the selector 'getFoo' 错误是 - 'geoMainViewController'没有可见的@interface声明选择器'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. 我确信我错过了一步,因为我对Objective C很新。我只是不确定我做错了什么。

Any help on this would be great. 对此的任何帮助都会很棒。

Thanks! 谢谢!

geoMainViewController.h您应该声明选择器是可见的:

-(NSString *)getFoo;

Did you put - (NSString *)getFoo in your geoMainViewController.h ? 你在你的geoMainViewController.h放了- (NSString *)getFoo 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. 您必须通过.h文件使这些方法对对象外部可见,因此其他对象知道它们响应哪些选择器。 Did the autoComplete fill in the message per chance? autoComplete是否每次都填写邮件?

#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) 编辑:(顺便说一句,你也可以让Foo成为一个属性)

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. 头文件包含.h文件中的所有函数声明,您只在类中包含.h文件。 So it depends on .h file. 所以这取决于.h文件。 .h file will have all the functions as the .m file. .h文件将所有函数都作为.m文件。

Hope it helps you. 希望它能帮到你。

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. 我怀疑你的实际错误是一个missinh方法声明,但你有更大的问题需要先解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM