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

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

问题描述

是否可以在 Objective-C 中声明诸如 Delegates 之类的匿名实现.我认为我的术语是正确的,但这是一个 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 在同一个类中:

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 中没有办法做到这一点.Apple 已经发布了一些关于向语言添加块(实际上更像 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天全站免登陆