I have two classes, a View and a ViewController. The View creates a button as follows:
_button = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, buttonImage.size.width, buttonImage.size.height)];
[_button setImage:buttonImage forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:@"btn_learn_focus_pad"] forState:UIControlEventTouchUpInside];
[_button addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[_someView addSubview:_button];
In the ViewController, after I instantiated the view in loadView, I add a selector to is as follows:
[view.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
The selector is not being called. I tried to NSLog in the ViewController the button’s position and it printed out a zero. I suspect that ARC is releasing the button. _button however, is a strong pointer (@property). So I am not sure why this is happening.
How do I get around this?
You are not utilizing the proper getter/setter methods generated by the property synthesis. If you just change the first line to:
It will properly retain the pointer