2017-07-03 3 views
0

Ich versuche, eine Schaltfläche programmgesteuert zu erstellen. Wenn es gedrückt wird, möchte ich, dass es eine Funktion aufruft, aber das funktioniert nicht. Button erscheint perfekt.addTarget UIButton funktioniert nicht

func createSubject(name: String, color: UIColor, fromLoad: Bool){ 
    createVC(name: name) 
    let button : UIButton = UIButton(frame: createButtonCGRect()) 
    UIView .animate(withDuration: 0.5, delay: 0.1, options: .curveEaseIn, animations: ({ 
     button.setTitle(name, for: .normal) 
     button.layer.cornerRadius = button.frame.width/2 
     button.backgroundColor = color 
     button.setTitle(name, for: .application) 
     button.isOpaque = false 
     //button.addTarget(self, action: #selector(self.presentVC(button:)), for: .touchUpInside) 
     //button.addTarget(self, action: #selector(self.deleteSubject(selector:)), for: .touchUpInside) 
     button.addTarget(self, action: #selector(self.addToWord), for: .touchUpInside) 
     button.setTitleColor(UIColor.white, for: .normal) 
     view!.addSubview(button) 
    }), completion: nil) 
    if (fromLoad == false){ 
     save().saveSubject(button: button) 
     disappearConfiguration() 
    }else{ 
     UIView .animate(withDuration: 0.5, animations: { 
      self.placeAddButton() 
      addButton?.isUserInteractionEnabled = true 
      addButton?.alpha = 1 

     }) 
    } 
} 

@objc func addToWord(selector: UIButton!){ 
    print("HOOOLAA") 
} 

Danke alle

+2

versuchen Sie es mit button.addTarget (self, action: #selector (self.addToWord (selector :)), für: .touchUpInside) Sind Sie sicher, dass diese Zeile ausgeführt wird? 'addButton? .isUserInteractionEnabled = true' –

Antwort

-1

den Code unten versuchen und

button.addTarget(self,action:#selector(self.addToWord(_:)), forControlEvents:.TouchUpInside) 

viel Glück überprüfen.

Verwandte Themen