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.
views:
32answers:
2
A:
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.
Axeva
2010-10-20 15:12:15
The 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 Wiles
2010-10-22 04:12:46
Post your code. Maybe we (the Stack Overflow community) can find the bug by looking at it.
Axeva
2010-10-24 01:19:09
my code is below.
Cory Wiles
2010-10-25 01:02:42
A:
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
Cory Wiles
2010-10-24 13:47:48