2017-06-16 7 views
0
addButton = UIButton(type: .custom) 
    addButton.setTitle("add", for: .normal) 
    addButton.setTitle("tapped", for: .highlighted) 
    addButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) 
    addButton.backgroundColor = UIColor.lightGray 
    addButton.translatesAutoresizingMaskIntoConstraints = false 
    addButton.addTarget(self, action: #selector(dosome(_:)), for: .touchUpInside) 
    let bottom_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.bottom, relatedBy:NSLayoutRelation.equal, toItem:mainView, attribute:NSLayoutAttribute.bottom, multiplier:1.0, constant: -10) 
    let right_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.right, relatedBy:NSLayoutRelation.equal, toItem:mainView, attribute:NSLayoutAttribute.right, multiplier:1.0, constant: -20) 
    let height_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.height, relatedBy:NSLayoutRelation.equal, toItem:nil, attribute:.notAnAttribute, multiplier:1.0, constant: 20) 
    let width_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.width, relatedBy:NSLayoutRelation.equal, toItem:nil, attribute:.notAnAttribute, multiplier:1.0, constant: 35) 
    addButton.addConstraint(height_button) 
    addButton.addConstraint(width_button) 
    mainView.addSubview(addButton) 
    mainView.addConstraint(bottom_button) 
    mainView.addConstraint(right_button) 

@IBAction func dosome(_ sender: UIButton) { 
    print("tttttt") 
} 

タップすると、ボタンのテキストが強調表示されたタイトルになり、何もしなくなります。誰でも何が間違っているのかを指摘できますか?応答なしでUIButtonをタップする

+0

メインビューは、ポップオーバーで表示されるUIViewです。 ButtonのisUserInteractionEnabledおよびそのスーパービューはtrueを返します。 – uuuy1798

答えて

0

使用addButton.isHighlighted = falseこれは動作します。

+0

助けてくれてありがとう。しかし、それはまだ動作していません。 defalut .isHighlightedは偽です – uuuy1798

+0

@ uuuy1798スクリーンショットを表示してください.. –

+1

ありがとうございます。私はオブジェクトを破壊する原因となる問題を修正しました。 – uuuy1798

0

この、addButton = UIButton(type: .system)を試してみて、私はこれを試してみたtheButton.adjustsImageWhenHighlighted = false;

+0

ありがとうございます。私は最終的に上記のコードで間違っていないことがわかります。オブジェクトを保持していないため、関数に到達できません。 – uuuy1798

0

設定し、それが正常に動作します。制約を削除しました。要件に応じて制約を追加できます。このaddButtonの上にtapGestureまたはuserInteractionEnabledビューがなく、IBActionメソッドが呼び出されていないことを確認してください。

 let addButton = UIButton(type: .custom) 
     addButton.setTitle("add", for: .normal) 
     addButton.setTitle("tapped", for: .highlighted) 
     addButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) 
     addButton.backgroundColor = UIColor.lightGray 
     addButton.translatesAutoresizingMaskIntoConstraints = false 
     addButton.addTarget(self, action: #selector(self.dosome(_:)), for: .touchUpInside) 

     self.view.addSubview(addButton) 


     func dosome(_ sender: UIButton) { 
     print("tttttt") 
     } 
+0

ありがとうございます。私は最終的に上記のコードで間違っていないことがわかります。オブジェクトを保持していないため、関数に到達できません。 – uuuy1798

関連する問題