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

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

问题描述

我是Objective-C的初学者程序员。任何人都可以告诉我如何在Objective-C中执行回调函数。



我只想看一些完整的例子,我应该明白了。非常感谢

解决方案

通常,目标C中的回调是由委托完成的。下面是自定义委托实现的示例:

  ///头文件
@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 MyClass
- (void)setDelegate:(id)aDelegate {
delegate = aDelegate; ///不保留
}

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

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



接下来你有一些对象是MyClass的委托,MyClass在适当的时候调用委托的委托方法。如果你的委托回调是可选的,你通常会在调度站点保护他们,比如if([delegate responsesToSelector:@selector(myClassWillDoSomething :)){。在我的例子中,代理需要实现这两种方法。



而不是一个非正式的协议,你也可以使用用@protocol定义的一个正式协议。如果你这样做,你可以改变委托设置器和实例变量的类型为 id< MyClassDelegate> 而不是 id



此外,您会注意到委托不会保留。这通常是因为拥有MyClass实例的对象通常也是委托。如果MyClass保留其委托,那么将有一个保留周期。在一个具有MyClass实例的类的dealloc方法中是一个好主意,它是它的委托来清除该委托引用,因为它是一个弱返回指针。否则,如果某些东西使MyClass实例存活,你将有一个悬空指针。


I am a beginner programmer in Objective-C. Can anyone please tell me how to perform call back functions in Objective-C.

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

Thanks a lot

解决方案

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

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

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


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

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

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.

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.

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".

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天全站免登陆