
[英]UIButton class fails to recognize selector after adding as a subview to a UIView
[英]Adding UIbutton as subview fails to call the Selector when pressed
这是我的代码
cell.imageView.contentMode = .ScaleAspectFit
cell.imageView.image = UIImage(named: "dummyImage")
cell.button.backgroundColor = UIColor.grayColor()
cell.button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
cell.button.titleLabel?.font = UIFont.systemFontOfSize(8.0)
cell.button.setTitle("PRESS HERE", forState: .Normal)
cell.button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.
cell.imageView.addSubview(cell.button)
该按钮正在显示并覆盖图像,但是选择器
压
当我单击按钮时没有被呼叫。 救命。
不喜欢
cell.imageView.addSubview(cell.button)
喜欢并尝试这个
cell.imageView.userInteractionEnabled = true
cell.button.frame=CGRectMake(cell.imageView.frame.origin.x, cell.imageView.frame.origin.y, cell.imageView.frame.size.width, cell.imageView.frame.size.height)
cell.button.tag = indexpath.row
cell.button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents. TouchUpInside)
cell.contentview.addSubview(cell.button)
然后像
func pressed(sender:UIButton)
{
NSLog("tapped index==%@",sender.tag)
}
pressed:
表示您正在调用的方法中有一个选择器。
尝试“按下”或将您的功能更改为:
func pressed(sender:UIButton!)
{
println("Button tapped")
}
尝试添加:
cell.imageView.userInteractionEnabled = true
UIImageView
此属性默认情况下为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.