I have a custom UIButton added as subview to a UIView subclass. In my viewController I add the custom uiview as a subview to the controller's view. Then I try to add a target to uibutton (inside viewWillLoad), but the selector is never called.
5 Answers
I just needed to move all logic that was in layoutSubviews to the constructor.

- 556
- 1
- 6
- 21
-
I have the same problem dude , can u paste ur solution code pls ? – Coldsteel48 Apr 03 '14 at 11:08
Make sure you're doing something like this:
UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 100, 40);
[btn setTitle:@"Click Me" forState: UIControlStateNormal];
[btn setBackgroundColor: [UIColor blackColor]];
[btn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[btn addTarget:self action:@selector(buttonTap:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:btn];
This will create a button that calls the buttonTap method when it's clicked.

- 4,697
- 6
- 40
- 64
-
1The target is a view controller method. I add the target for the button in the view controller. My problem is the UIControlEvent isn't being "registered". – Cory D. Wiles Oct 22 '10 at 04:12
-
Post your code. Maybe we (the Stack Overflow community) can find the bug by looking at it. – Axeva Oct 24 '10 at 01:19
Here is the custom view and the view controller logic
// custom view
@implementation CustomUIASView
@synthesize firstButton;
@synthesize secondButton;
@synthesize thirdButton;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.alpha = .85;
self.backgroundColor = [UIColor blackColor];
}
return self;
}
- (void)layoutSubviews {
UIImage *buttonImageNormal;
UIImage *stretchableButtonImageNormal;
//
buttonImageNormal = [UIImage imageNamed:@"as-bg.png"];
stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
self.firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.firstButton.frame = CGRectMake(10, 10, 300, 50);
self.firstButton.backgroundColor = [UIColor clearColor];
[self.firstButton setTitleColor:RGB(16,62,30) forState:UIControlStateNormal];
[self.firstButton setTitle:@"Watch Video" forState:UIControlStateNormal];
[self.firstButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
self.firstButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
[self addSubview:self.firstButton];
self.secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.secondButton.frame = CGRectMake(10, (10 + 50 + 5), 300, 50);
self.secondButton.backgroundColor = [UIColor clearColor];
[self.secondButton setTitleColor:RGB(16,62,30) forState:UIControlStateNormal];
[self.secondButton setTitle:@"Save to My Favorites" forState:UIControlStateNormal];
[self.secondButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
self.secondButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
[self addSubview:self.secondButton];
self.thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
buttonImageNormal = [UIImage imageNamed:@"social-button-bg.png"];
stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
self.thirdButton.frame = CGRectMake(10, (secondButton.frame.origin.y + secondButton.frame.size.height + 5), 300, 50);
self.thirdButton.backgroundColor = [UIColor clearColor];
[self.thirdButton setTitleColor:RGB(16,62,30) forState:UIControlStateNormal];
[self.thirdButton setTitle:@"Share with Friends on" forState:UIControlStateNormal];
[self.thirdButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[self.thirdButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
CGRect thirdButtonFrame = thirdButton.titleLabel.frame;
self.thirdButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
self.thirdButton.titleEdgeInsets = UIEdgeInsetsMake(thirdButtonFrame.origin.x, thirdButtonFrame.origin.y + 20, 0, 0);
[self addSubview:self.thirdButton];
[super layoutSubviews];
}
- (void)dealloc {
[firstButton release];
[secondButton release];
[thirdButton release];
[super dealloc];
}
@end
// view controller snippet
self.uiasView = [[CustomUIASView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height + 270), kDefaultFrameWidth, 300)];
[self.uiasView.firstButton addTarget:self action:@selector(pushToRunwayViewController:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.uiasView];
"pushToRunwayViewController" is never called

- 556
- 1
- 6
- 21
-
2I forgot to post my solution. Everything works I just needed to remove all logic from layoutSubviews to initWithFrame. Now the method in my controller is called. – Cory D. Wiles Jan 13 '11 at 22:41
-
The view lifecycle and delegate methods are odd getting used to. Thanks for posting this solution, I was having the same problem and this nipped it. I guess layoutSubviews is called after the viewControllers viewDidLoad method. +1 – atreat Feb 21 '12 at 20:31
Not sure if you figured out for yourself but when you addTarget:self to your firstButton, self is your actual custom view, not the view controller.
So if you move your (pushToRunwayViewController) method to CustomAISView everything should work as expected.
I hope this helps. Rog

- 18,602
- 6
- 76
- 97
-
self does refer to the view controller. The purpose of my exercise was to create a custom "action sheet" that would be used throughout an app without tying controller specific implementation to it. Having to put the pushToRunwayViewController in the class would have broken that abstraction. However I got everything working (see comment above). I'll be posting a blog about it soon. – Cory D. Wiles Jan 13 '11 at 22:44
First, UIView is different from UIControl. UIView does not know specifically how to treat different user interaction and does not implement most of the event handling method.
UIControl does that for you. Its notable subclass is UIButton. Try to subclass from UIControl to get all the methods for event handling. For a more complex way, inside that subclass of UIView subclass another UIControl but i don't think its a good idea.
More a little bit advanced event handling and message passing look at this:
How to add UIViewController as target of UIButton action created in programmatically created UIView?

- 1
- 1

- 1,545
- 12
- 20