I have a custom UIButton a开发者_如何学Pythondded 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.
I just needed to move all logic that was in layoutSubviews to the constructor.
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.
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
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
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?
精彩评论