Objective C将类指针传递给静态库,然后从类中调用方法

时间:2013-08-25 15:35:08

标签: iphone ios objective-c

您好,感谢您提前提供的任何帮助。我试着搜索,但似乎我想做的事情并不常见。如果有另一篇帖子谈论我的问题,请链接。

要开始,我有两节课。为了保持名称简单,一个叫做Controller,另一个叫我的AppDelegate。控制器是一个静态库。 AppDelegate使用以下内容创建控制器的实例。

*注意:我已经在.h文件中声明了_controller。

_controller  = [[Controller alloc] initWithAppDelegate:self];

我想做的是从控制器调用AppDelegate类中的方法。 controller.h类中的相关代码如下。

@interface
{
  id _appDelegate;
}

-(id) initWithAppDelegate:(id)appDelegate;

并在controller.m

-(void)someMethodName
{
  [_appDelegate method];
}

我遇到的问题是该方法无法调用。我可以调用其他几种方法,但不能调用我在AppDelegate中创建的自定义方法。我已经尝试用AppDelegate替换ID,但我似乎无法导入AppDelegate,我有点理解为什么我无法导入它。

我以这种方式创建项目的原因是我想在具有为不同设备设计的UI的应用程序中重用控制器代码。如果其中任何一项不清楚或您需要更多信息,请随时提出。

感谢。

2 个答案:

答案 0 :(得分:0)

在静态库中声明这样的协议:

@protocol AppDelegateRequiredMethods
@required
... methods here ...
@end

然后,将声明该协议的标头导入各种AppDelegate实现,并将您的app委托声明为实现所述协议:

@interface MyAppDelegate:NSObject <AppDelegateRequiredMethods>
...
@end

答案 1 :(得分:0)

Controller不了解app委托或其方法。您必须使用协议声明接口。

@protocol AppDelegateMethods
- (void)method;
@end

//Then replace your current id with     
id <AppDelegateMethods>