iPhone - 在通用类中引用应用程序委托
我正在编写一个通用类,可以通过链接在不同的项目中使用。
在某个时刻,我在一个侦听器上调用一个方法,该侦听器由拥有该对象的侦听器提供,并通过分配保留到类中。
但有时,该调用者可能会消失,因此在这种情况下我希望将返回消息路由到应用程序委托。
以下是我为调用者所做的事情(调用者是创建并拥有我的类实例的人):
if ([self.responseListener respondsToSelector:@selector(serverAnswered:error:)]) {
// some job to construct the return object
[self.responseListener performSelector:@selector(serverAnswered:error:) withObject:response withObject:nil];
}
当调用者消失时,如何引用应用程序委托类来代替responseListener?
I a writing a generic class that may be used in different projects by link.
At some moment, I call a method on a listener given by the one that owns the object and kept by assign into the class.
But sometimes, that caller may disapear, so I want in that case to route the return message to the application delegate.
Here is how I do for the caller (the caller is the one that created and owns the instance of my class) :
if ([self.responseListener respondsToSelector:@selector(serverAnswered:error:)]) {
// some job to construct the return object
[self.responseListener performSelector:@selector(serverAnswered:error:) withObject:response withObject:nil];
}
How may I reference the app delegate class in place of responseListener when the caller has disapeared ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定“当来电者消失时”中的“来电者”是什么意思。不过,您可以通过以下方式从任何地方访问应用程序委托。
如果您需要调用特定应用程序委托特有的方法,则需要导入它并进行强制转换。
更新:
要在任何应用委托上调用您自己的库方法,请使用协议。
当您指定库用户需要实现哪些方法时,这将使您的 API 变得清晰,并且是一个很好的解决方案,因为它允许编译时检查而不是依赖于运行时动态消息发送。
您还可以跳过协议,直接调用协议上的方法。
I'm not sure what you mean with "caller" in "when the caller has disappeared". Here's how you can access the application delegate from anywhere, though.
If you need to call methods on it unique to your particular application's delegate, you need to import it and cast.
Update:
To call your own library methods on any app delegate, use a protocol.
This will make your API clear as you specify what methods your library users need to implement, and is a good solution as it allows for compile time checks instead of relying on run time dynamic message sending.
You can also skip the protocol, and call methods on it directly.