【问题标题】:Programatically added button action not being called未调用以编程方式添加的按钮操作
【发布时间】:2017-12-14 06:14:35
【问题描述】:

我正在通过 viewForFooterInSection 的委托方法在我的 TableView 控制器中添加一个静态页脚视图,它工作正常,它正在添加我所需的设计,就像我想要的那样,但在按钮上的 touchUpInside 上没有调用操作。我已经通过堆栈并尝试了几乎所有方法都无济于事。我可能做错了什么。这是我的代码

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = UIColor.white

    let blackView = UIView()
    blackView.frame = CGRect(x: 0, y: 51, width: self.view.frame.size.width, height: 2)
    blackView.backgroundColor = UIColor.black
    view.addSubview(blackView)

    let button = UIButton()
    button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 51)
    button.titleLabel?.font = UIFont.init(name: "Lato-Bold", size: 22)
    button.titleLabel?.textColor = UIColor.black
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)        
    view.addSubview(button)
    return view
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 53
}

@objc func buttonAction(sender: UIButton!) {
    print("Button tapped")
}

【问题讨论】:

  • 我刚刚在 Playground 上对其进行了测试,它工作正常我在控制台中获得了 Button Tapped

标签: ios swift uitableview uibutton


【解决方案1】:

我正在尝试检查您的代码,addTarget 将被触发

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.gray

        let blackView = UIView()
        blackView.frame = CGRect(x: 0, y: 51, width: self.view.frame.size.width, height: 2)
        blackView.backgroundColor = UIColor.red
        view.addSubview(blackView)

        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 51)
        button.titleLabel?.font = UIFont.init(name: "Lato-Bold", size: 22)
        button.titleLabel?.textColor = UIColor.black
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        view.addSubview(button)
        return view
      }

      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 53
      }

      @objc func buttonAction(sender: UIButton!) {
        print("Button tapped working fine")
      }

【讨论】:

  • 纯代码答案没有帮助。解释问题所在并解释此答案如何解决问题。
  • 这段代码有什么不同?我看不出有什么区别。
【解决方案2】:

我试过你的代码。工作正常我刚刚从方法定义中删除了 override 关键字,因为我使用的是 Swift 4.0 及更高版本。请在您的代码中检查以下给定的内容,因为它可能会影响您的按钮点击问题。

1.检查是否有任何透明视图没有覆盖您的 tableView 页脚。

2。检查层次结构中任何视图的userInteractionIsEnabled 是否为false

3.如果您在该层次结构中添加了任何视图,请检查点击手势。

【讨论】:

    【解决方案3】:

    以上问题的解决方案在这里

    在你的类中创建一个按钮操作函数buttonAction(_ sender: UIButton) 添加按钮作为视图的子视图后,将操作分配给按钮为button.addTarget(self, action: #selector(self.buttonAction(_ :)), for: .touchUpInside) 以获取详细信息,我在下面添加了代码sn-p

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.white
    
        let blackView = UIView()
        blackView.frame = CGRect(x: 0, y: 51, width: self.view.frame.size.width, height: 2)
        blackView.backgroundColor = UIColor.black
        view.addSubview(blackView)
    
        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 51)
        button.titleLabel?.font = UIFont.init(name: "Lato-Bold", size: 22)
        button.titleLabel?.textColor = UIColor.black
        view.addSubview(button)
        button.addTarget(self, action: #selector(self.buttonAction(_ :)), for: .touchUpInside)        
        return view
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 53
    }
    
    func buttonAction(_ sender: UIButton) {
        print("Button tapped")
    }
    

    希望对你有帮助

    【讨论】:

    • 纯代码答案没有帮助。解释问题所在并解释此答案如何解决问题。
    • 好的,谢谢@rmaddy 的建议
    • 这与问题中的代码有何不同?该问题已经有一个有效的选择器和一个有效的匹配按钮操作方法。您所有的答案更改是将_ 参数标签添加到sender 参数。这如何解决问题(提示:它没有)?
    • @GaneshManickam 不需要添加框架,因为委托方法已经返回了一个视图,并且 heightForFooter 也返回了高度,所以这不是问题。
    【解决方案4】:

    我检查了你的代码,我发现视图框架有问题

    添加这一行,

    view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 53)
    

    所以代码会是这样的,

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let view = UIView()
    
    view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 53)
    
    view.backgroundColor = UIColor.white
    
    let blackView = UIView()
    blackView.frame = CGRect(x: 0, y: 51, width: self.view.frame.size.width, height: 2)
    blackView.backgroundColor = UIColor.black
    view.addSubview(blackView)
    
    let button = UIButton()
    button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 51)
    button.titleLabel?.font = UIFont.init(name: "Lato-Bold", size: 22)
    button.titleLabel?.textColor = UIColor.black
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)        
    view.addSubview(button)
    return view
    

    }

    【讨论】:

    • 问题中的选择器没有问题。这个答案不会改变任何事情。这是其他答案的重复。发布重复的答案没有意义。
    • 你能检查我更新的答案吗? view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 53)
    • 我发现视图没有框架(宽高),所以它添加了零框架
    【解决方案5】:

    好吧,我没有得到积分。尝试修改这两个地方:
    1. #selector(buttonAction) -> #selector(self.buttonAction(_ :))。 /
    2. @objc func buttonAction(sender: UIButton!) -> 删除@objc
    如果不工作,自定义单元格并实现目标动作。它一定可以工作。

    【讨论】:

    • 不要发布两个答案。编辑一个答案并删除另一个答案。
    【解决方案6】:

    你的选择器名称缺少参数,使用 #selector(buttonAction(sender:)) 替换 #selector(buttonAction)

    【讨论】:

    • 问题中的选择器没有问题。这个答案不会改变任何事情。
    • 好吧,我没有得到积分。尝试修改这两个地方: 1.#selector(buttonAction) -> #selector(self.buttonAction(_ :)) 2.@objc func buttonAction(sender: UIButton!) -> 删除@objc。如果没有工作,自定义单元格,它必须工作。
    • 编辑你的答案,不要发布 cmets。但是方法名或者选择器都没有错。
    • 我的英语不好,如果你看不懂我的话,就再等等吧~。
    • 再一次,不要在 cmets 中发布代码,将信息放在答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2015-06-30
    相关资源
    最近更新 更多