【问题标题】:Swift add show action to button programmaticallySwift以编程方式将显示动作添加到按钮
【发布时间】:2016-06-03 17:18:54
【问题描述】:

如何以编程方式向按钮添加操作。我需要向 mapView 中的按钮添加显示操作。谢谢

let button = UIButton(type: UIButtonType.Custom) as UIButton

【问题讨论】:

  • button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)
  • 这只会导致错误@MuhammadRaheelMateen
  • 请分享错误日志

标签: ios iphone swift


【解决方案1】:

您需要像 Muhammad 建议那样向按钮添加目标

button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)

但你还需要一个方法来执行该操作

func action(sender: UIButton) {
    // Do whatever you need when the button is pressed
}

【讨论】:

    【解决方案2】:
      let button = UIButton(type: UIButtonType.Custom) as UIButton
      button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)
    
      //then make a action method :
    
      func action(sender:UIButton!) {
         print("Button Clicked")
      }
    

    【讨论】:

    • 出于某种原因,在 Swift 3 中,Xcode 建议 button.addTarget(self, action: #selector(getter: UIDynamicBehavior.action) 不推荐使用 Objective C 选择器的字符串文字选择器。我想这是正确的..
    • This answer 的语法略有不同;它对我有用,而这些没有。 YMMV。
    【解决方案3】:

    你可以去下面的代码
    `

    let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
    btn.backgroundColor = UIColor.green
    btn.setTitle("Click Me", for: .normal)
    btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    btn.tag = 1
    self.view.addSubview(btn) 
    

    行动

     @objc func buttonAction(sender: UIButton!) {
            let btnsendtag: UIButton = sender
            if btnsendtag.tag == 1 {
    
                dismiss(animated: true, completion: nil)
            }
        }
    

    【讨论】:

      【解决方案4】:

      对于 Swift 4,请使用以下内容:

      button.addTarget(self, action: #selector(AwesomeController.coolFunc(_:)), for: .touchUpInside)
      
      //later in your AswesomeController
      @IBAction func coolFunc(_ sender:UIButton!) {
        // do cool stuff here
      }
      

      【讨论】:

        【解决方案5】:
        override func viewDidLoad() {
            super.viewDidLoad()  
        
            let btn = UIButton()
            btn.frame = CGRectMake(10, 10, 50, 50) 
            btn.setTitle("btn", forState: .Normal) 
            btn.setTitleColor(UIColor.redColor(), forState: .Normal) 
            btn.backgroundColor = UIColor.greenColor() 
            btn.tag = 1
            btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside)  //add button action
            self.view.addSubview(btn)
        }
        

        【讨论】:

          【解决方案6】:

          除了以上,新的ios14介绍

           if #available(iOS 14.0, *) {
               button.addAction(UIAction(title: "Click Me", handler: { _ in
                              print("Hi")
                          }), for: .touchUpInside)           
           } else {
               // Fallback on earlier versions
           }
          

          【讨论】:

            【解决方案7】:

            对于 swift 5 用户可以通过这种简单的方式完成

            cell.followButton.tag = 10
            cell.followButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
            
            @objc func buttonAction(sender: UIButton!) {
               let id = sender.tag
               print(id)
            }
            

            【讨论】:

              猜你喜欢
              • 2021-10-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-19
              • 2021-10-12
              相关资源
              最近更新 更多