开发者

UIButton addTarget: self ... causes "Does not recognize selector" error

I initialised my UIButton-deriver liek this:

Button * it = [[Button alloc] initW开发者_StackOverflow社区ithFrame: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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜