Objective C - 来自另一个类的调用方法 [英] Objective C - Call method from another class

查看:76
本文介绍了Objective C - 来自另一个类的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个班级 geoViewController geoMainViewController

我在 geoMainViewController 中有一个方法叫做 getFoo

I have a method in the geoMainViewController called getFoo

它看起来像这样:

- (NSString *)getFoo
{

NSString* foo = @"This is foo";

return foo;

}

我试图打电话给 getFoo 来自 geoViewController 类。

I am trying to call getFoo from the geoViewController class.

我有 #import我的 geoViewController m文件中的geoMainViewController.h

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

我正在尝试实例化 geoMainViewController 类并从我的 geoViewController getFoo 方法$ c>这样的类:

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];    

}

它似乎是在实例化 geoMainViewController 类很好,但我在 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'没有可见的@interface声明选择器'getFoo'

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

我确信我错过了一步,因为我很新目标C.我只是不确定我做错了什么。

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.

谢谢!

推荐答案

在你的 geoMainViewController.h 中你应该声明选择器可见:

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

-(NSString *)getFoo;

这篇关于Objective C - 来自另一个类的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆