-1

I define a function of point

typedef void (^ButtonClick)(id sender);

and i want to call it when Button click(UIButton addTarget to call ^ButtonClick function) but it is could find the pointer.

-(void)addRightButton:(UIImage*)btnImage click:(ButtonClick)click{
    UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [modalViewButton addTarget:self
      action:@selector(click) // <==== could not find pointer.Pointer errors
          forControlEvents:UIControlEventTouchUpInside];
    // other code to add modelViewButton on View.....
}

-(void)test
{ 
    [self addRightButton:[UIImage imageNamed:@"btn_shuaxin_1.png"] click:^(id sender) {
         NSLog(@"it is test code");//<===never called
    }];
}

how to make it to SEL?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
zt9788
  • 948
  • 4
  • 16
  • 31
  • Now function, method, selector or block? –  Nov 07 '12 at 05:38
  • i do not understand what is your mean. – zt9788 Nov 07 '12 at 05:43
  • The problem is that neither do I understand what you asked. The question is incomprehensible and does not quite make sense inits current form, please consider rephrasing it. –  Nov 07 '12 at 05:45
  • Sorry, my English is not very good,and i fixed my question. – zt9788 Nov 07 '12 at 05:48
  • Oh, I see now. `@selector()` is not good for this, it's only good for obtaining constant selectors. I'll write an answer. –  Nov 07 '12 at 05:51

1 Answers1

0

You can't get selectors like this. (For getting selectors at runtime, use the sel_getUid() function). Also, you're confusing selectors and blocks. What you want is possible, but using a different approach:

- (void)addRightButton:(UIImage *)btnImage click:(ButtonClick)click{
    UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [modalViewButton addTarget:click
        action:@selector(invoke)
        forControlEvents:UIControlEventTouchUpInside];
}

- (void)test
{ 
    [self addRightButton:[UIImage imageNamed:@"btn_shuaxin_1.png"] click:^{
        NSLog(@"it is test code");
    }];
}
  • thanks very much ,it is work.but I need to change "typedef void (^ButtonClick)(id sender)" to "typedef void (^ButtonClick)()" I do not modify the pointer function can? – zt9788 Nov 07 '12 at 06:01
  • @zt9788 yes you do, you can't get the sender using this approach. But please do one favor for me: learn C and Objective-C. Basically you don't know what the difference is between a selector, a function and a block... This is not good! –  Nov 07 '12 at 06:03
  • thanks you very much.i will learn it. – zt9788 Nov 07 '12 at 06:06