繁体   English   中英

按下时将UIbutton添加为子视图无法调用选择器

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM