Objective-C中的匿名委托实现?

时间:2023-01-15 18:20:13

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:

是否可以在Objective-C中声明像委托这样的匿名实现。我想我的术语是对的,但这里有一个java例子:

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

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:

例如,要处理一个UIActionSheet调用我必须在同一个类中声明另一个方法,如果我想要传递数据,这看起来有点傻,因为我必须把数据存储为全局变量。这里有一个删除某些内容的例子,在确认对话框中询问您是否确定:

-(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];
}

and the UIActionSheetDelegate in the same class:

和同一个类中的UIActionSheetDelegate:

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

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?

我想要做的是将它声明为内联,就像我在顶部的java示例中那样。这是可能的吗?

5 个解决方案

#1


17  

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.

目前在Objective-C中没有办法做到这一点。苹果公司已经发表了一些关于在语言中添加块(实际上更像lambda闭包而不是匿名类)的工作。你可能会做一些类似匿名委托的事情。

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:

与此同时,大多数Cocoa程序员都将委托方法添加到委托类的单独类别中。这有助于使代码更有条理。在您的示例中的类的.m文件中,我将这样做:

@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's method popup in the editor window will separate the category's declaration and implementation from the main class'.

在编辑器窗口中弹出的Xcode方法将类别的声明和实现从主类中分离出来。

#2


4  

Objective-C doesn't have a notion of anonymous classes like Java's, so you can't create a class "inline" like in the Java code.

Objective-C不像Java那样有匿名类的概念,所以不能像Java代码那样创建“内联”类。

#3


2  

I was looking for something different when I came across this but if you do a search for UIALERTVIEW+BLOCKS you will find several hits for doing inline UIALERTVIEWs. This is the one I've been using: ALERTVIEW w/blocks

当我遇到这个的时候,我在寻找一些不同的东西但是如果你搜索UIALERTVIEW+BLOCKS你会发现内联UIALERTVIEW会有几个点击。这是我一直在使用的:ALERTVIEW w/blocks。

#4


1  

I believe that anonymous classes can be implemented in Objective-C, but it will take a lot of NSProxy magic and IMP madness. This is one of my current projects.

我相信匿名类可以在Objective-C中实现,但是它需要大量的NSProxy魔法和IMP疯狂。这是我目前的一个项目。

#5


0  

How about a class implementing the delegate interface. On initialization it would take a block. In the delegate definition it calls this block.

实现委托接口的类怎么样?在初始化时,它需要一个块。在委托定义中,它调用这个块。

This allows multiple UIActionSheets to exist simultaneously without having to compare on identity.

这允许多个UIActionSheets同时存在,而不需要对身份进行比较。

#1


17  

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.

目前在Objective-C中没有办法做到这一点。苹果公司已经发表了一些关于在语言中添加块(实际上更像lambda闭包而不是匿名类)的工作。你可能会做一些类似匿名委托的事情。

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:

与此同时,大多数Cocoa程序员都将委托方法添加到委托类的单独类别中。这有助于使代码更有条理。在您的示例中的类的.m文件中,我将这样做:

@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's method popup in the editor window will separate the category's declaration and implementation from the main class'.

在编辑器窗口中弹出的Xcode方法将类别的声明和实现从主类中分离出来。

#2


4  

Objective-C doesn't have a notion of anonymous classes like Java's, so you can't create a class "inline" like in the Java code.

Objective-C不像Java那样有匿名类的概念,所以不能像Java代码那样创建“内联”类。

#3


2  

I was looking for something different when I came across this but if you do a search for UIALERTVIEW+BLOCKS you will find several hits for doing inline UIALERTVIEWs. This is the one I've been using: ALERTVIEW w/blocks

当我遇到这个的时候,我在寻找一些不同的东西但是如果你搜索UIALERTVIEW+BLOCKS你会发现内联UIALERTVIEW会有几个点击。这是我一直在使用的:ALERTVIEW w/blocks。

#4


1  

I believe that anonymous classes can be implemented in Objective-C, but it will take a lot of NSProxy magic and IMP madness. This is one of my current projects.

我相信匿名类可以在Objective-C中实现,但是它需要大量的NSProxy魔法和IMP疯狂。这是我目前的一个项目。

#5


0  

How about a class implementing the delegate interface. On initialization it would take a block. In the delegate definition it calls this block.

实现委托接口的类怎么样?在初始化时,它需要一个块。在委托定义中,它调用这个块。

This allows multiple UIActionSheets to exist simultaneously without having to compare on identity.

这允许多个UIActionSheets同时存在,而不需要对身份进行比较。