如何在 Objective-C 中执行回调 [英] How to perform Callbacks in Objective-C

查看:52
本文介绍了如何在 Objective-C 中执行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Objective-C中执行回调函数?

How to perform call back functions in Objective-C?

我只是想看一些完整的例子,我应该能理解.

I would just like to see some completed examples and I should understand it.

推荐答案

通常,目标 C 中的回调是通过委托完成的.这是自定义委托实现的示例;

Normally, callbacks in objective C are done with delegates. Here's an example of a custom delegate implementation;

头文件:

@interface MyClass : NSObject {
    id delegate;
}
- (void)setDelegate:(id)delegate;
- (void)doSomething;
@end

@interface NSObject(MyDelegateMethods)
- (void)myClassWillDoSomething:(MyClass *)myClass;
- (void)myClassDidDoSomething:(MyClass *)myClass;
@end

<小时>

实现 (.m) 文件


Implementation (.m) File

@implementation MyClass
- (void)setDelegate:(id)aDelegate {
    delegate = aDelegate; /// Not retained
}

- (void)doSomething {
    [delegate myClassWillDoSomething:self];
    /* DO SOMETHING */
    [delegate myClassDidDoSomething:self];
}
@end

这说明了一般方法.您在 NSObject 上创建一个类别,声明您的回调方法的名称.NSObject 实际上并没有实现这些方法.这种类型的类别称为非正式协议,您只是说许多对象可能会实现这些方法.它们是一种转发声明选择器类型签名的方法.

That illustrates the general approach. You make a category on NSObject that declares the names of your callback methods. NSObject doesn't actually implement these methods. This type of category is called an informal protocol, you're just saying that many objects might implement these methods. They're a way to forward declare the type signature of the selector.

接下来,您有一些对象是MyClass"的委托,MyClass 会根据需要调用委托上的委托方法.如果您的委托回调是可选的,您通常会在调度站点使用类似if ([delegate RespondsToSelector:@selector(myClassWillDoSomething:)) {"的内容来保护它们.在我的示例中,委托需要实现这两种方法.

Next you have some object be the delegate of "MyClass" and MyClass calls the delegate methods on the delegate as appropriate. If your delegate callbacks are optional, you'll typically guard them at the dispatch site with something like "if ([delegate respondsToSelector:@selector(myClassWillDoSomething:)) {". In my example, the delegate is required to implement both methods.

您还可以使用由@protocol 定义的正式协议来代替非正式协议.如果这样做,您会将委托 setter 和实例变量的类型更改为id ",而不仅仅是id".

Instead of an informal protocol, you can also use a formal protocol defined with @protocol. If you do that, you'd change the type of the delegate setter, and instance variable to be "id <MyClassDelegate>" instead of just "id".

此外,您会注意到委托并未保留.这通常是因为拥有"MyClass"实例的对象通常也是委托.如果 MyClass 保留了它的委托,那么就会有一个保留周期.在具有 MyClass 实例的类的 dealloc 方法中,这是一个好主意,并且是清除该委托引用的委托,因为它是弱后向指针.否则,如果有什么东西使 MyClass 实例保持活动状态,您将有一个悬空指针.

Also, you'll notice the delegate is not retained. This is typically done because the object that "owns" instances of "MyClass" is typically also the delegate. If MyClass retained its delegate, then there would be a retain cycle. It's a good idea in the dealloc method of a class that that has a MyClass instance and is its delegate to clear that delegate reference since it's a weak back pointer. Otherwise if something is keeping the MyClass instance alive, you'll have a dangling pointer.

这篇关于如何在 Objective-C 中执行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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