0

This code worked in a UIView container, but after changing that container to a UIViewController, the code no longer works:

-(void)viewDidLoad 
{
    [super viewDidLoad];

    [self.view setUserInteractionEnabled:YES];

    // set button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(60, 30, 220, 220);
    [button setImage:self.obj.img forState:UIControlStateNormal];
    [button addTarget:self action:@selector(selected:) forControlEvents:UIControlEventTouchUpInside];
    [button setUserInteractionEnabled:YES];

    [self.view addSubview:button];
}

-(void) selected:(UIButton *) button // also tried (id) sender
{
    NSLog(@“Button Selected“);
}

These containers are created programmatically (dynamically), from web services, so I cannot use Storyboard. The reason for changing to a UIViewController is that I need a segue to another screen. Also, these containers are placed in a UIScrollView.

Everything else is working properly, except the selector.

Setting the userInteraction wasn't necessary earlier, but I've gone ahead and tried that in different configurations. I've added this to the scrollview, the viewController, and the button, just for good measure.

I even tried some online tutorials for programmatically creating UIButtons in UIViewControllers and was able to do that just fine. Not sure what the issue is here. Thanks.

Dev
  • 13
  • 4
  • All code complete, but i think you can't tap on button. – Pramod Tapaniya Feb 27 '16 at 09:46
  • [button setImage: forState:UIControlStateHighlighted]; try this and check while you tap on button anotherImage display? – Pramod Tapaniya Feb 27 '16 at 09:50
  • Changing the state to Highlighted only causes the image to flash when the button is clicked. – Dev Feb 27 '16 at 09:58
  • Hmmm @Dev, so actual problem is you can't tap on the button, because of may be any control display upper on button with clean color.. Please verify. – Pramod Tapaniya Feb 27 '16 at 10:02
  • Did you try using "Debug View Hierarchy" button to see if there is any other view on the top? [link](http://stackoverflow.com/a/26052806/2000162) Another reason...you have gesture recogniser on the same view which is being called first. – TomCobo Feb 27 '16 at 10:10
  • In the View Hierarchy, the image is 'in front' of the button. But they are the top-most layers. The button has no response if the first gesture line is removed (or set to 'NO'). – Dev Feb 27 '16 at 10:31

0 Answers0