【问题标题】:Adding UIbutton as subview fails to call the Selector when pressed按下时将 UIbutton 添加为子视图无法调用 Selector
【发布时间】:2016-02-17 10:04:26
【问题描述】:

这是我的代码

                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)

按钮正在显示并覆盖图像,但选择器

按下

当我点击按钮时没有被调用。帮助。

【问题讨论】:

  • 你能发图片吗?您是否将按钮添加为 uiimage 的子视图?
  • 不需要这个cell.imageView.addSubview(cell.button),如果你想为图片点击操作
  • @Anbu.Karthik 如果我不添加 cell.imageView.addSubview(cell.button) ,按钮会被图像隐藏

标签: ios swift uibutton uiimage


【解决方案1】:

不喜欢

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)
}

【讨论】:

  • 谢谢。 cell.contentview.addsubView(cell.button) 完成了这项工作。
【解决方案2】:

pressed: 表示您正在调用的方法中有一个选择器。

尝试使用“按下”或将您的功能更改为:

func pressed(sender:UIButton!)
{
    println("Button tapped")
}

【讨论】:

    【解决方案3】:

    尝试添加:

    cell.imageView.userInteractionEnabled = true
    

    UIImageView 的这个属性默认为false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多