iPhone - 在通用类中引用应用程序委托

发布于 2024-11-27 14:16:35 字数 506 浏览 1 评论 0原文

我正在编写一个通用类,可以通过链接在不同的项目中使用。
在某个时刻,我在一个侦听器上调用一个方法,该侦听器由拥有该对象的侦听器提供,并通过分配保留到类中。
但有时,该调用者可能会消失,因此在这种情况下我希望将返回消息路由到应用程序委托。

以下是我为调用者所做的事情(调用者是创建并拥有我的类实例的人):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2024-12-04 14:16:35

我不确定“当来电者消失时”中的“来电者”是什么意思。不过,您可以通过以下方式从任何地方访问应用程序委托。

[UIApplication sharedApplication].delegate;

如果您需要调用特定应用程序委托特有的方法,则需要导入它并进行强制转换。

#import "MyAppDelegate.h"

// ...

MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;

更新

要在任何应用委托上调用您自己的库方法,请使用协议。

// The app delegate in your library users app
#import "YourFancyLibrary.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate, YourFancyLibraryDelegate>

// In YourFancyLibrary.h, declare that protocol
@protocol YourFancyLibraryDelegate
- (void)myFancyMethod;
@end

// Refer to it in the guts of your library.
id<YourFancyLibraryDelegate> delegate = [UIApplication sharedApplication].delegate;
if (![delegate conformsToProtocol:@protocol(YourFancyLibraryDelegate)]) return;
if (![delegate respondsToSelector:@selector(myFancyMethod)]) return;
[delegate myFancyMethod];

当您指定库用户需要实现哪些方法时,这将使您的 API 变得清晰,并且是一个很好的解决方案,因为它允许编译时检查而不是依赖于运行时动态消息发送。

您还可以跳过协议,直接调用协议上的方法。

id appDelegate = [UIApplication sharedApplication].delegate;
SEL methodToCall = @selector(someMethod);
if ([appDelegate respondsToSelector:methodToCall]) {
    [appDelegate performSelector:methodToCall];
}

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.

[UIApplication sharedApplication].delegate;

If you need to call methods on it unique to your particular application's delegate, you need to import it and cast.

#import "MyAppDelegate.h"

// ...

MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;

Update:

To call your own library methods on any app delegate, use a protocol.

// The app delegate in your library users app
#import "YourFancyLibrary.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate, YourFancyLibraryDelegate>

// In YourFancyLibrary.h, declare that protocol
@protocol YourFancyLibraryDelegate
- (void)myFancyMethod;
@end

// Refer to it in the guts of your library.
id<YourFancyLibraryDelegate> delegate = [UIApplication sharedApplication].delegate;
if (![delegate conformsToProtocol:@protocol(YourFancyLibraryDelegate)]) return;
if (![delegate respondsToSelector:@selector(myFancyMethod)]) return;
[delegate myFancyMethod];

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.

id appDelegate = [UIApplication sharedApplication].delegate;
SEL methodToCall = @selector(someMethod);
if ([appDelegate respondsToSelector:methodToCall]) {
    [appDelegate performSelector:methodToCall];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文