2016-08-02 3 views
0

나는 스위프트에 아주 새롭다. 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가 업데이트되지 않는다는 것입니다. 그래서 격차는 여전히 존재합니다. 거기에 어떤 해결책이 있습니까? – 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

이 함수는 "잡히지 않는 예외"오류가 발생합니다. – ripa

+0

글쎄,이 대답은 당신이 어딘가에 당신의 견해를 저장하고 있으며 최종 답이 아닌 가이드가되기위한 것이라는 가정에 근거합니다. 내가 도와 줄 수 있도록 그 오류에 대한 자세한 내용을 알려줘야합니다. – Armin

+0

libC++ abi.dylib : NSException 유형의 catch되지 않은 예외로 종료합니다. -이 오류 – ripa

관련 문제