1

I initialised my UIButton-deriver liek this:

Button * it = [[Button alloc] initWithFrame:CGRectMake(x, y, image.size.width, image.size.height)];

Then, I do the next:

[(UIButton *)self addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self];

The first line causes "Does not recognize selector" error.

Selector buttonClicked: looks like this:

-(IBAction) buttonClicked:(id)sender { 
    if (action) action();
    else NSLog(@"Clicked.\n");
} 

What am I doing wrong?

Zero.Arcan
  • 51
  • 3

2 Answers2

1

Add the action with:

[it       addTarget:self 
             action:@selector(buttonClicked:) 
   forControlEvents:UIControlEventTouchUpInside];

and then add the button with

[view addSubview:it];

And don't create UIButtons with init... use the class method + buttonWithType:.

Be careful to to subclass UIButton, I am not sure if you really want this. Have a look at create uibutton subclass.

Community
  • 1
  • 1
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
0

You create buttons with +(id)buttonWithType:(UIButtonType)buttonType

Simon Lee
  • 22,304
  • 4
  • 41
  • 45