Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous delegate implementation in Objective-C?

Tags:

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     } }); 

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

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

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?

like image 259
rustyshelf Avatar asked Apr 20 '09 00:04

rustyshelf


2 Answers

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.

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

like image 178
Barry Wark Avatar answered Sep 22 '22 06:09

Barry Wark


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.

like image 29
mipadi Avatar answered Sep 23 '22 06:09

mipadi