Xcode for How to call function from an class to another class function in iPhone Apps

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8799240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:57:34  来源:igfitidea点击:

Xcode for How to call function from one class to another class function in iPhone Apps

objective-ciosxcodeios4xcode4

提问by ManojRupareliya

JewelsSharedController.m:

JewelsSharedController.m:

-(void)GetCountries:(id)sender detailDictionary:(id)details
{
}

and I want to call this function in RootViewController's function like
RootController.m:

我想调用这个函数 RootViewController类似的功能
RootController.m:

-(void)soapresult
{
    //i want to call ""GetContries "" here..
}

and i m call this "soapresult"in to

我打电话"soapresult"

-(void)viewdidLoad
{
    [self soapresult];
}

but when calling function its crash

但是当调用函数时它崩溃了

回答by Kjuly

JewelsSharedController.h:

JewelsSharedController.h:

//...
-(void)GetCountries:(id)sender detailDictionary:(id)details;
//...

RootController.m:

根控制器.m:

#import "JewelsSharedController.h"

//...

-(void)soapresult
{
    JewelsSharedController * jewelsSharedController = [[JewelsSharedController alloc] init];
    [jewelsSharedController GetCountries:yourSender detailDictionary:yourDetails];
    [jewelsSharedController release];
}

回答by Armand

Change this

改变这个

-(void)GetCountries:(id)sender detailDictionary:(id)details

{

{

}

}

To

+(void)GetCountries:(id)sender detailDictionary:(id)details

{

{

}

}

And then to call it in another class simply do

然后在另一个类中调用它简单地做

[JewelsSharedController GetCountries];

What we are doing is creating a class function, what that means is you can call the function directly on the class without having to initialize an instance of the class first.

我们正在做的是创建一个类函数,这意味着您可以直接在类上调用该函数,而无需先初始化类的实例。