添加UIView子类中添加的UIButton的目标在UIViewController上不起作用
我将自定义UIButton作为子视图添加到UIView子类中。在我的viewController中,我将自定义uiview作为子视图添加到控制器的视图中。然后我尝试将目标添加到uibutton(inside viewWillLoad),但从未调用选择器。请确保您正在执行以下操作:添加UIView子类中添加的UIButton的目标在UIViewController上不起作用,uiview,uiviewcontroller,uibutton,Uiview,Uiviewcontroller,Uibutton,我将自定义UIButton作为子视图添加到UIView子类中。在我的viewController中,我将自定义uiview作为子视图添加到控制器的视图中。然后我尝试将目标添加到uibutton(inside viewWillLoad),但从未调用选择器。请确保您正在执行以下操作: UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 1
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方法。请确保执行以下操作:
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方法。以下是自定义视图和视图控制器逻辑 //自定义视图
@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”从未被调用这是自定义视图和视图控制器逻辑 //自定义视图
@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”永远不会被调用不确定您是否自己解决了问题,但当您将target:self添加到第一个按钮时,self是您实际的自定义视图,而不是视图控制器 因此,如果您将(pushToRunwayViewController)方法移动到CustomAISView,一切都应按预期工作 我希望这有帮助。
Rog不确定您是否自己找到了答案,但当您将target:self添加到第一个按钮时,self是您实际的自定义视图,而不是视图控制器 因此,如果您将(pushToRunwayViewController)方法移动到CustomAISView,一切都应按预期工作 我希望这有帮助。
Rog我只需要将布局子视图中的所有逻辑移动到构造函数。我只需要将布局子视图中的所有逻辑移动到构造函数。首先,UIView不同于UIControl。UIView不知道如何具体处理不同的用户交互,也不实现大多数事件处理方法 这是为你做的。它显著的子类是UIButton。尝试从UIControl子类化以获取事件处理的所有方法。对于更复杂的方式,在UIView的子类中,有另一个UIControl子类,但我认为这不是一个好主意 更多关于高级事件处理和消息传递的信息请参见:
首先,UIView不同于UIControl。UIView不知道如何具体处理不同的用户交互,也不实现大多数事件处理方法 这是为你做的。它显著的子类是UIButton。尝试从UIControl子类化以获取事件处理的所有方法。对于更复杂的方式,在UIView的子类中,有另一个UIControl子类,但我认为这不是一个好主意 更多关于高级事件处理和消息传递的信息请参见:
目标是视图控制器方法。我在视图控制器中添加按钮的目标。我的问题是uicontrol事件未“注册”。请发布您的代码。也许我们(堆栈溢出社区)可以通过查看来找到bug。目标是一个视图控制器方法。我在视图控制器中添加按钮的目标。我的问题是uicontrol事件未“注册”。请发布您的代码。也许我们(Stack Overflow社区)可以通过查看来找到bug。我忘记发布我的解决方案了。一切正常,我只需要删除LayoutSubView到initWithFrame的所有逻辑。现在我的控制器中的方法被调用了。视图生命周期和委托方法已经习惯了。感谢您发布此解决方案,我遇到了相同的问题,这就解决了它。我猜layoutSubviews是在viewControllers viewDidLoad方法之后调用的+1我忘了发布我的解决方案。一切正常,我只需要删除LayoutSubView到initWithFrame的所有逻辑。现在我的控制器中的方法被调用了。视图生命周期和委托方法已经习惯了。感谢您发布此解决方案,我遇到了相同的问题,这就解决了它。我猜layoutSubviews是在viewControllers viewDidLoad方法之后调用的+1self指的是视图控制器。我的练习的目的是创建一个自定义的“操作表”,它将在整个应用程序中使用,而无需绑定特定于控制器的实现。必须将pushToRunwayViewController放在类中会破坏这种抽象。然而,我让一切正常(见上面的评论)。我很快会在博客上发布相关内容。self确实指的是视图控制器。我的练习的目的是创建一个自定义的“操作表”,它将在整个应用程序中使用,而无需绑定特定于控制器的实现。必须将pushToRunwayViewController放在类中会破坏这种抽象。然而,我让一切正常(见上面的评论)。我很快就会发布一篇关于它的博客。我有同样的问题,伙计,你能粘贴你的解决方案代码吗?我有同样的问题,伙计,你能粘贴你的解决方案代码吗?