2

I have a NSDictionary that holds the set of objects that will be used in an array to create as many as needed. I have a delete button in the upper right corner of the image and for some reason, I'm unable to click it. I'll write it out...

-(void)addItemsToSomething:(NSMutableArray*)array
{

    for (NSDictionary *item in array)  {

    UIImageView *container....

    UIImageView *boxInContainer....

    UIButton *delete = [UIButton new];
    delete.frame = CGRectMake(70, -4, 24, 26);
    delete.userInteractionEnabled = YES;
    [delete setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [delete addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [boxInContainer addSubview:delete];

    some math....
}
more math
}

-(void)delete:(id)sender
{
    NSLog(@"Do I work");
}

The NSLog never shows in the logs nor does the button shadow to show it's been touched. I even set the userInteractionEnabled to YES just to try and cover another base but the button still stares me in the face and won't click. Thanks for the help.

This is all placed on a scrollview as well, forgot that part.

TomG103
  • 311
  • 2
  • 26
  • Where do you add the button to the view? – Martin Koles Jun 30 '14 at 19:47
  • 1
    The devil is in the details. Post your actual code that creates the button, adds a target, and adds it to your view. Also, even though when you add a target/action in code you can get away without making your action method an IBAction, you should still make it an IBAction anyway, to document what it's used for. – Duncan C Jun 30 '14 at 19:47
  • Check the "delete" button frame. You can have a zero-sized frame but the button is still visible as its subviews/sublayers are not clipped. Obviously if it has a zero-sized or really small frame the hit-test will fail. Or better: show us the code you used to create the button. – viggio24 Jun 30 '14 at 19:48
  • This sort of issue is often cause by the sort of thing I discuss here: http://stackoverflow.com/a/22188740/341994 – matt Jun 30 '14 at 19:51
  • It sounds like something is on top of the button, double check your view hierarchy in XCode storyboard – meda Jun 30 '14 at 20:11

2 Answers2

2

Problem is that You place it on UIImageView. UIImageView has userInteractionEnabled set to NO by default. Simply change boxInContainer.userInteractionEnabled = YES; and button should work!

Oh, and if boxInContainer is subview of container, then You must also set container.userInteractionEnabled = YES;

Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
  • 1
    Can someone vote Guntis Treulands answer, it worked. Thanks Guntis. The userinteraction was the key, now all is good in my xcode. – TomG103 Jun 30 '14 at 20:53
1

I think, that your parent container view is out of his superView's bounds. To check that, try so set clipToBounds property to YES for superview of your parent container view, and see, is still your button visible?

arturdev
  • 10,884
  • 2
  • 39
  • 67