2016-08-02 17 views
0

私は非常に新しいですswift。私はsubviewを使ってmultiple textfield and buttonを作成しました。私ViewControllerの出力は以下の通りです: - hereボタンをクリックしたときにボタンを削除する方法は?

は、今私は"-"ボタンを削除する必要があり、それがクリックされたとき、それはtextfield対応です。 しかし、どのボタンがクリックされたのかを検出することはできません。 これは私のコードです:

var y: CGFloat = 190 
var by: CGFloat = 192 

@IBAction func addRow(sender: AnyObject) { 

    y += 30 
    by += 30 

    let textFiled = UITextField(frame:CGRectMake(50.0, y, 100.0, 20.0)) 

    textFiled.borderStyle = UITextBorderStyle.Line 

    let dunamicButton = UIButton(frame:CGRectMake(155.0, by, 15.0, 15.0)) 
    dunamicButton.backgroundColor = UIColor.clearColor() 
    dunamicButton.layer.cornerRadius = 5 
    dunamicButton.layer.borderWidth = 1 
    dunamicButton.layer.borderColor = UIColor.blackColor().CGColor 
    dunamicButton.backgroundColor = .grayColor() 
    dunamicButton.setTitle("-", forState: .Normal) 
    dunamicButton.addTarget(self, action: #selector(removeRow), forControlEvents: .TouchUpInside) 




    self.view.addSubview(textFiled) 
    self.view.addSubview(dunamicButton) 
} 


func removeRow(sender: UIButton!) { 
    print("Button tapped") 
    self.view.removeFromSuperview() 
} 

任意の助けをいただければ幸いです...

+0

ボタンのタグを使って試しましたか? –

+1

あなたはurコードを表示できます –

+0

私のコードを追加しました – ripa

答えて

1

EMKAは正しかったです!これを試してみてください:

import UIKit 

class ViewController: UIViewController { 

    var y:CGFloat = 100 
    var textFields = [UIButton : UITextField]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    @IBAction func onAddMoreButtonPressed(sender: AnyObject) { 
     let newButton = UIButton(frame: CGRect(x: 50, y: y, width: 150, height: 20)) 


     newButton.setTitle("New button", forState: .Normal) 
     newButton.backgroundColor = UIColor.blueColor() 
     newButton.addTarget(self, action: #selector(ViewController.onNewButtonPressed(_:)), forControlEvents: .TouchUpInside) 

     self.view.addSubview(newButton) 

     let newTextField = UITextField(frame: CGRect(x: 200, y: y, width: 150, height: 20)) 
     newTextField.text = "New text field" 
     self.view.addSubview(newTextField) 

     textFields[newButton] = newTextField 

     y += 20 
     if y > self.view.frame.height { 
      y = 100 
     } 
    } 

    func onNewButtonPressed(sender: UIButton) { 
     textFields[sender]?.removeFromSuperview() 
     sender.removeFromSuperview() 
    } 

} 
+0

このコンセプトはうまくいきます。しかし問題はYが更新されていないことです。ギャップはまだそこにあります。 rこれにはどんな解決策がありますか? – ripa

+0

更新していないものは?表示、辞書? – eMKa

+0

ビューこれをチェックしてください - [http://imgur.com/a/dbOKx] – ripa

0

あなたは任意のビューコントローラのメソッドtouchesBeganをオーバーライドすることができます。あなたはどこか

let buttons : [UIButton] = [] 

次の操作を行うことができ、あなたのボタンの配列を格納していると仮定すると:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    super.touchesBegan(touches, withEvent: event) 

    guard let touch: UITouch = touches.first else { 
     return 
    } 

    for button in buttons { 
     if touch.view == button { 
      print("This is the button you tapped") 
      button.removeFromSuperview() 
     } 
    } 

} 
+0

私はこの機能で "uncaught例外"エラーを得ています。 – ripa

+0

この回答は、あなたの意見をどこかに保存しており、最終的な答えではなくガイドになるという前提に基づいています。私があなたを助けることができるように、そのエラーの詳細を述べる必要があります。 – Armin

+0

libC++ abi.dylib:NSExceptionタイプのキャッチされていない例外で終了する - このエラー – ripa

関連する問題