Objective-C中的匿名委托实现? [英] Anonymous delegate implementation in Objective-C?

查看:169
本文介绍了Objective-C中的匿名委托实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Objective-C中声明代理的匿名实现。我想我的术语是正确的,但这里是一个java示例:

Is it possible to declare anonymous implementations of things like Delegates in Objective-C. I think I have the terminology right, but here's a java example:

myClass.addListener(new FancyInterfaceListener({
    void onListenerInterestingAction(Action a){
        ....interesting stuff here
    }
});

所以例如处理一个UIActionSheet调用我必须在同一个类中声明另一个方法,这似乎有点傻,如果我想传递它的数据,因为我必须存储该数据作为一个全局变量,这里有一个删除一些确认对话框的例子,询问你是否确定:

So for example to handle an UIActionSheet call I have to declare another method in the same class, which seems a bit silly if I want to pass it data, because I'd have to store that data as a global variable. Here's an example of deleting something with a confirmation dialog asking you if your sure:

-(void)deleteItem:(int)indexToDelete{
    UIActionSheet *confirm = [[UIActionSheet alloc] initWithTitle:@"Delete Item?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
    [confirm showInView:self.view];
    [confirm release];
}

和UIActionSheetDelegate在同一个cla中ss:

and the UIActionSheetDelegate in the same class:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        [[Settings sharedSettings] removeItemAtIndex:/*need index variable here*/];
        [drinksTable reloadData];
    }
}

我想要做的是声明在线,就像我在java示例中顶部一样。这是可能的吗?

What I want to be able to do is declare it inline, just like I did in the java example at the top. Is this possible?

推荐答案

目前没有办法在Objective-C中执行此操作。苹果已经发表了一些关于他们为语言添加块(更像是lambda闭包而不是匿名类)的工作。您可能可以使用这些匿名委托进行类似的操作。

There is no way to do this in Objective-C currently. Apple has published some work on their efforts to add blocks (really more like lambda closures than anonymous classes) to the language. You would likely be able to do something similar to the anonymous delegate with those.

同时,大多数Cocoa程序员将代理方法添加到委托单独的类别中类。这有助于保持代码更有条理。在您的示例中的类的.m文件中,我将执行以下操作:

In the mean time, most Cocoa programmers add the delegate methods to a separate category on the delegate class. This helps to keep the code more organized. In the .m file for the class in your example, I would do something like this:

@interface MyClass (UIActionSheetDelegate)
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
@end

@implementation MyClass
//... normal stuff here
@end

@implementation MyClass (UIActionSheetDelegate)
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        [[Settings sharedSettings] removeItemAtIndex:/*need index variable here*/];
        [drinksTable reloadData];
    }
}
@end

Xcode的方法弹出编辑器窗口将把类别的声明和实现与主类'。

Xcode's method popup in the editor window will separate the category's declaration and implementation from the main class'.

这篇关于Objective-C中的匿名委托实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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