@selector() in Swift?

后端 未结 23 2532
清酒与你
清酒与你 2020-11-21 15:24

I\'m trying to create an NSTimer in Swift but I\'m having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), us         


        
23条回答
  •  隐瞒了意图╮
    2020-11-21 16:05

    // for swift 2.2
    // version 1
    buttton.addTarget(self, action: #selector(ViewController.tappedButton), forControlEvents: .TouchUpInside)
    buttton.addTarget(self, action: #selector(ViewController.tappedButton2(_:)), forControlEvents: .TouchUpInside)
    
    // version 2
    buttton.addTarget(self, action: #selector(self.tappedButton), forControlEvents: .TouchUpInside)
    buttton.addTarget(self, action: #selector(self.tappedButton2(_:)), forControlEvents: .TouchUpInside)
    
    // version 3
    buttton.addTarget(self, action: #selector(tappedButton), forControlEvents: .TouchUpInside)
    buttton.addTarget(self, action: #selector(tappedButton2(_:)), forControlEvents: .TouchUpInside)
    
    func tappedButton() {
      print("tapped")
    }
    
    func tappedButton2(sender: UIButton) {
      print("tapped 2")
    }
    
    // swift 3.x
    button.addTarget(self, action: #selector(tappedButton(_:)), for: .touchUpInside)
    
    func tappedButton(_ sender: UIButton) {
      // tapped
    }
    
    button.addTarget(self, action: #selector(tappedButton(_:_:)), for: .touchUpInside)
    
    func tappedButton(_ sender: UIButton, _ event: UIEvent) {
      // tapped
    }
    

提交回复
热议问题