从AppDelegate Objective-C IOS调用someClass中的方法 [英] calling a method inside someClass from AppDelegate Objective-C IOS

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

问题描述

我正在尝试从AppDelegate类中调用someClass中的方法。
我通常创建一个实例,然后使用该实例,调用该方法。像这样:

I'm trying to call a method that's inside someClass from the AppDelegate class. I usually create an instance and then using that instance, call the method. Like so:

FFAppDelegate *delegate = (FFAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate someMethod];

我在代码中使用了^^,它完美无缺。我想做的是切换它。我没有调用AppDelegate中的方法INSIDE,而是想在AppDelegate的另一个类中调用一个方法。

I use that ^^ quite a bit in my code and it works perfectly. What I want to do, is switch it around. Instead of calling a method INSIDE the AppDelegate, I want to call a method inside another class FROM the AppDelegate.

SomeClass *test = (SomeClass *) [[UIApplication sharedApplication] delegate];
[test someMethod];

在这种情况下,我不断收到因未捕获的异常而终止应用程序'NSInvalidArgumentException'错误无法识别的选择器被发送到实例。

In this case, I keep getting "Terminating app due to uncaught exception 'NSInvalidArgumentException'" error due to "unrecognized selector sent to instance".

任何关于此事的灯光都将不胜感激! :)

Any light shed on the matter would be greatly appreciated! :)

推荐答案

实例化要从中发送请求的类的实例,并使方法成为公共的(在。 h文件)。然后将该类导入应用程序委托并调用它。

Instantiate an instance of the class that you want to send the request from and make the method public (in the .h file). Then import that class into the app delegate and call it.

像这样......

  YourClass * yourClassInstance = [[YourClass alloc] init];
  [yourClassInstance someMethod];

在@interface下面的YourClass.h中你会声明这样的方法

in YourClass.h below the @interface you would declare the method like so

 -(void)someMethod;

所以任何人都可以访问它。

So anyone could access it.

这篇关于从AppDelegate Objective-C IOS调用someClass中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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