Three reasons why UIButton does not respond

A button (UIButton object) sometimes does not respond. And there are three reasons as for me.
(1)because the button is put out of the superview frame
(2)because the button is hidden by other transparent UIView object
(3)because delegate is set as target

The other day a button did not respond in my developing code and I was finding the reason. There were many views on which the button is put, therefore I thought the reason must be (1) but I was not able to fix it. Finally I found the reason and it was because of (2), the button was just hidden by transparent UIView object. I wasted much time.

About (3)
[button addTarget:_delegate action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];
↑You usually write this kind of code to set UIButton object. When I set delegate as target, this sometimes did not work.
When you try to write code as MVC pattern, you usually write code to put UIButton in VIEW. And you send a message to CONTROLLER when tapped.
Therefore I wrote code as above (set delegate as target) but failed. I could not find the reason, though it works when you set self as target and the method send message to delegate.
    //put button
    [button addTarget:self action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];

-(void)tappedButton:(UIButton *)button
    [_delegate tappedButton:button];
Yes this works but looks waste of code.
Develop | Comments(0) | Trackback(0)