-1

I have looked at various answers on SO for this but can't quite get my head around how it all actually works.

What I have is a GameEngine that contains touchable elements, what I want is for when an element is touched it fires off a "I have been touched" event that the GameEngine listens for and deals with appropriately.

In C# I'd do this with delegates/events but can't seem to find a decent obj c equivalent that uses blocks. - I want to use Blocks as it more akin to anonymous functions in C# which I am used to.

In C# I'd simply do something like the following, in objective c it seems I need to write a page of code to get the same thing working?

touchableObject.touched += (o) => { handler code yay }

Edit based on Driis' answer:

Declaration

typedef void (^CircleTouchedHandler)(id parameter);

@interface CircleView : UIView{
}

@property CircleTouchedHandler Touched;

How to call it and pass myself as a parameter?

[self Touched(self)]; // this doesnt work
Chris
  • 26,744
  • 48
  • 193
  • 345
  • http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1 might be helpful for you. – mah Nov 09 '13 at 13:28
  • show us the page of code you need. – vikingosegundo Nov 09 '13 at 13:30
  • Why the downvote? Looking at this answer here is seems like a LOT of extra stuff is required, all Im after is the most succinct way of doing it that matches with the way I already do things in other languages http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – Chris Nov 09 '13 at 13:31
  • 2
    Or, perhaps a little safer, `if (self.Touched) self.Touched(self);` because if `Touched` is `nil`, you'd crash (because, while you can safely send a message to `nil` (which does nothing), you cannot invoke a `nil` block). Also, you might want to follow [Cocoa property naming conventions](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/codingguidelines/Articles/NamingIvarsAndTypes.html), using a lowercase letter, e.g. `touched` instead of `Touched`. Personally, I'd call it something like `touchHandler` or `touchBlock`. – Rob Nov 09 '13 at 14:13
  • That property should be `copy`, too. – bbum Nov 09 '13 at 18:09

1 Answers1

1

In Objective-C, that would look something like:

touchableObject.touched = ^(id o) { /* handler code */ };

Assuming touched is a property of an appropiate block type. If you re-use a block type (think of Func in C#), it makes sense to typedef it, since a block declaration in ObjC tends to become difficult to read very quickly.

The block syntax gets some time to get used to in Objective-C when you are coming from another language with slightly more elegant block/lambda syntax. To learn and as a reference, see this previous answer, which has helped me a lot.

To typedef a type for touched, you would use something like:

typedef void (^Handler)(id parameter);

Then simply declare the touched property as type handler.

Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341
  • This looks promising - How do I define touched? – Chris Nov 09 '13 at 13:30
  • How do I actually call the callback? '[self Touched(self)];' doesnt work? (I updated my question based on your feedback) – Chris Nov 09 '13 at 13:41
  • Note that unlike C#'s events, this particular implementation allows only a single callback to be registered. You could, however, implement a register method and back it with an array or dictionary of blocks to support multiple callbacks. – devios1 Dec 02 '14 at 22:54