【问题标题】:button created programmatically doesn't respond to target, swift以编程方式创建的按钮不响应目标,迅速
【发布时间】:2015-03-28 19:47:55
【问题描述】:

我正在第 1 节的 tableView 标题中创建一个按钮。

我是这样做的:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
        header.backgroundColor = UIColor.greenColor()
    if section == 1 {
        var button   = UIButton()
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
        header.addSubview(button)
    }
        return header
}

 func Action(sender: UIButton){
    println("button was pressed")
}

该按钮仅出现在第 1 部分(应该如此),但如果我按下它而不是打印消息,则不会发生任何事情。

【问题讨论】:

    标签: swift button header tableview frame


    【解决方案1】:

    我的问题是我没有正确指定 button.frame。由于该按钮位于框架之外,因此它从未被调用过。

     button.frame = CGRectMake(5, 2, tableView.frame.size.width - 5, 18);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2015-02-04
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多