UIButton event 'Touch Up Inside' not worki

2019-07-23 12:18发布

问题:

I've got a UIButton in a UIView, which is in a UIViewController that I've allocated and inited from a .xib file.

When hooking an IBAction up to the 'Touch Down' event of a UIButton it triggers correctly. But if I hook it up to the 'Touch Up Inside' event it doesn't respond.

Other events don't work either. 'Touch Drag Enter', 'Touch Drag Exit', 'Touch Up Outside' etc. Only Touch Down.

Does anyone have any idea why this might be?

Rich

回答1:

First, I can absolutely, positively guarantee you that UIButtons correctly connected in IB responds as expected to touch-up-inside events. (Can you imagine if they didn't?!)

Given that, it's a fair bet that you have not hooked things up as you expect. Here are some things to try:

  • Make your action print a log statement first thing:

    NSLog(@"%s", __PRETTY_FUNCTION__);

  • Check in IB that the ONLY function that your button is wired to is your touch-up-inside action. (Later, you can wire it to multiple events but, for testing, we want to simplify as much as possible.)

  • Make sure your button is 50x50 or bigger. (Small buttons are hard to press.)
  • For now, make your button a rounded-rect button. If you had an image button, and the image has transparency, it may be hard to press.
  • Make sure you have the correct signature for your action:

    - (IBAction) myButtonPressedAction: (id) sender;

  • Just to be sure everything's clean, delete the button from your .xib and re-add it.

  • Make sure the parent view has userInteractionEnabled set.
  • Make sure that the xib's FileOwner is your view-controller type.

If none of that helps, you'll have to tell us more about the problem area. Perhaps add a screenshot of your button setup in IB and code snippets from your project.