Iphone 如何获取UIButton目标、操作和控制事件?
我使用UIImageView的UIButtons一大堆。因此,我创建了一个定制类,将这两个类永久结合在一起,使事情变得简单一些。在我决定实现-(id)initWithObject:(AUIImageViewButton*)imageViewButton之前,一切都很好 显然,我需要从正在传递的imageViewButton对象复制所有相关属性。UIImageView根本没有问题。类似这样的东西可以解决这个问题:Iphone 如何获取UIButton目标、操作和控制事件?,iphone,Iphone,我使用UIImageView的UIButtons一大堆。因此,我创建了一个定制类,将这两个类永久结合在一起,使事情变得简单一些。在我决定实现-(id)initWithObject:(AUIImageViewButton*)imageViewButton之前,一切都很好 显然,我需要从正在传递的imageViewButton对象复制所有相关属性。UIImageView根本没有问题。类似这样的东西可以解决这个问题: imageview = [[UIImageView alloc] initWithF
imageview = [[UIImageView alloc] initWithFrame:imageViewButton.imageview.frame]; // Copy all relevant data from the source's imageview
[imagebutton.imageview setBackgroundColor:imageViewButton.imageview.backgroundColor]; //
[imagebutton.imageview setImage:imageViewButton.imageview.image]; //
大多数按钮材料也可随时提供:
button = [UIButton buttonWithType:imageViewButton.button.buttonType]; // Copy all relevant data from the source's button
button.frame = imageViewButton.imageview.frame; //
[button setTitle:imageViewButton.button.titleLabel.text forState:UIControlStateNormal]; //
button.tag = imageViewButton.button.tag; //
我在弄清楚如何获取addTarget:action:forControlEvents方法的所有数据时遇到了一些困难
查看文档,我可以看到我可能能够使用UIControl的allControlEvents和allTargets方法。我现在就深入研究一下,看看能给我带来多大的麻烦。我不确定的是行动
有人能帮我往正确的方向推吗
谢谢
-MartinUIControl的
所有目标
和所有控制事件
是开始的方式。拼图的最后一块是actionsForTarget:forControlEvent:
,为每个目标和事件调用一次。显示如何迭代一个按钮的目标并在另一个按钮上创建选择器的副本。具体的例子就是touchupinside事件,但这通常是我使用的全部
for (id target in button.allTargets) {
NSArray *actions = [button actionsForTarget:target
forControlEvent:UIControlEventTouchUpInside];
for (NSString *action in actions) {
[newButton addTarget:target action:NSSelectorFromString(action) forControlEvents:UIControlEventTouchUpInside];
}
}
在分配新目标/操作之前,我使用此选项删除任何可能不需要的目标/操作:
if let action = button.actions(forTarget: target, forControlEvent: .touchUpInside)?.first
{
button.removeTarget(target, action: NSSelectorFromString(action), for: .touchUpInside)
}
或者,如果确实要删除所有操作:
if let actions = button.actions(forTarget: target, forControlEvent: .touchUpInside)
{
for action in actions
{
button.removeTarget(target, action: NSSelectorFromString(action), for: .touchUpInside)
}
}
只要检查一下,您就知道UIButton支持背景图像(标题文本显示在顶部)和图像(没有标题文本显示)?您需要UIImageView的哪些功能?啊!就在那里!就在我面前!谢谢你指出这一点。