4

Possible Duplicate:
Can I pass a block as a @selector with Objective-C?

I have this code:

[myButton addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

But since doSomething contains so little code, I was wondering if it is possible to put the code straight in here rather than making it run doSomething.

I have tried this already:

[myButton addTarget:self action:^ { /* code here */ } forControlEvents:UIControlEventTouchUpInside];

But it just gives an error of "sending (void)^(void) to incompatible type SEL".

Is there a way to do this? If so what is the correct syntax?

Thanks!

Community
  • 1
  • 1
CHRIS
  • 957
  • 3
  • 10
  • 27

1 Answers1

6

Well, all of your answers use a lot of unnecessary code which is horribly outdated and no longer works.

I found one answer myself though which I adapted and got this:

[myButton addTarget:[^{NSLog(@"Pressed the button");} copy] action:@selector(invoke) forControlEvents:UIControlEventTouchUpInside];

Which works fine.

CHRIS
  • 957
  • 3
  • 10
  • 27