我有一个自定义UIButton作为子视图添加到UIView子类。在我的viewController中,我将自定义的uiview作为子视图添加到控制器的视图中。然后我尝试添加一个目标到uibutton(在viewWillLoad中),但是选择器从不被调用。添加目标UIButton添加到UIView子类不工作UIViewController
回答
我只需要将layoutSubviews中的所有逻辑移至构造函数。
确保你正在做这样的事情:
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];
这将创建一个按钮,单击时调用buttonTap方法。
目标是一个视图控制器方法。我在视图控制器中添加按钮的目标。我的问题是UIControlEvent没有被“注册”。 – 2010-10-22 04:12:46
发布您的代码。也许我们(堆栈溢出社区)可以通过查看它来发现错误。 – Axeva 2010-10-24 01:19:09
我的代码如下。 – 2010-10-25 01:02:42
这里是定制视图和视图控制器逻辑
//定制视图
@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
//视图控制器片断
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” 不会被调用
我忘了发布我的解决方案。我只需要将layoutSubviews中的所有逻辑移除到initWithFrame即可。现在我的控制器中的方法被调用。 – 2011-01-13 22:41:18
视图生命周期和委托方法很古怪。感谢发布这个解决方案,我遇到了同样的问题,并且将其揪住了。我猜layoutSubviews是在viewControllers viewDidLoad方法之后调用的。 +1 – atreat 2012-02-21 20:31:24
不知道你是否想出了自己,但当你添加T. arget:self到你的firstButton,self是你实际的自定义视图,而不是视图控制器。
因此,如果您将您的(pushToRunwayViewController)方法移动到CustomAISView,则所有内容都应按预期工作。
我希望这会有所帮助。 Rog
自我确实指的是视图控制器。我的练习的目的是创建一个自定义的“操作表”,该操作表将在整个应用程序中使用,而无需将控制器特定的实现与其绑定。必须将pushToRunwayViewController放入类中才能打破抽象。不过,我得到了一切工作(见上面的评论)。我会很快发布一篇关于它的博客。 – 2011-01-13 22:44:30
首先,UIView与UIControl不同。 UIView并不知道如何处理不同的用户交互,也没有实现大部分的事件处理方法。
UIControl为您做到了。其显着的子类是UIButton。尝试从UIControl子类获取事件处理的所有方法。对于更复杂的方法,在UIView子类的子类中,有另一个UIControl,但我不认为它是一个好主意。
更多一点点高级事件处理和消息传递看看这个:
How to add UIViewController as target of UIButton action created in programmatically created UIView?
- 1. 添加UIview到UIViewController
- 2. 将UIView添加到UIViewController
- 3. 将uiview添加到uiviewcontroller
- 4. 如果将UIView作为子视图添加,UIButton目标无法工作
- 5. 添加标签到UIView子类
- 6. 添加UIView子视图不工作
- 7. 将UICollectionViewController添加到UIViewController不工作
- 8. 添加CAShapeLayer到UIButton不工作
- 9. 添加的UIView上的UIViewController
- 10. 无法从UIViewController添加UIView
- 11. 用IBOutlet添加UIViewController子类
- 12. 为UIButton添加目标 - iOS
- 13. 在UIButton中添加UIView
- 14. 将UIButton添加到UIScrollView,然后将UIScrollView添加到UIView
- 15. 添加一个UIViewController到一个UIView
- 16. 将.xib文件添加到UIViewController子类?
- 17. 用UIButton添加UIView到UIWindow点击
- 18. 为UIButton添加目标不叫
- 19. 如何添加UIViewController作为编程添加子视图的目标,如IB
- 20. 添加的UIButton到子视图不工作
- 21. 在Swift中添加子类到UIView
- 22. 将多个UIImageView添加到子类UIView
- 23. 将目标添加到UIButton不起作用
- 24. 中的UIButton添加的CALayer不工作
- 25. UIButton类无法识别选择器后,作为子视图添加到UIView
- 26. 添加UIButton图标?
- 27. 用xib向UIViewController添加一个UIView子类
- 28. UITapGestureRecogniser添加目标不工作
- 29. jquery addClass添加子类不工作
- 30. 作为子视图添加UIButton远离UIView
我有同样的问题老兄,你可以粘贴你的解决方案代码吗? – 2014-04-03 11:08:32