2017-12-10 11 views
2

저는 Xcode/Swift로 프로그래밍하기가 매우 어려워서 작은 문제에 직면하고 있습니다.대리인은 항상 섹터없이 2x ViewControllers간에 정보를 전달하지 않습니다.

저는 대리자와 segue를 사용하지 않고 ViewController에서 두 번째 ViewController로 정보를 보내려고합니다. 나는 그것에 대해 많은 것을 읽었고 가장 일반적인 해결책이 ViewDidLoad에서 "인스턴스".delegate = self를 사용하고 있음을 발견했다. 그러나 그것은 단지 나를 위해 작동하지 않는다.

- 앱의 정의 -

꽤 쉽습니다. 첫 번째 ViewController에는 단추와 레이블이 있습니다. 버튼은 두 번째 ViewController를 엽니 다.이 두 번째 ViewController에는 textField 및 Button이 있습니다. Button는 textField에있는 내용을 첫 번째 ViewController로 보내 레이블을 업데이트합니다.

- 코드 -이 첫 번째의 ViewController 코드입니다

:

protocol clickedButtonDelegate { 
    func didClickedButton(text: String) 
} 

class SecondViewController: UIViewController { 

    @IBOutlet weak var introducedText: UITextField! 

    var delegate : clickedButtonDelegate! 

    @IBAction func sendData(_ sender: Any) { 
     if delegate != nil { 
      let information:String = introducedText.text! 
      delegate!.didClickedButton(text: information) 
      dismiss(animated: true, completion: nil) 
      self.navigationController?.popViewController(animated: true) 
     } 
    } 
} 

이 코드 아무것도됩니까 사용하지 :

class ViewController: UIViewController, clickedButtonDelegate { 

    @IBOutlet weak var Label: UILabel! 

    func didClickedButton(text: String) { 
     Label.text = text 
    } 

    var secondView = SecondViewController() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     secondView.delegate = self 
    } 
} 

이 두 번째의 ViewController에 대한 코드입니다 왜냐하면 대리자는 SecondViewController에서 항상 nil이기 때문입니다.

도와 주시겠습니까?

미리 감사드립니다.

+3

언제 어떻게 SecondViewController를 푸시합니까? 코드 조각도 제공해 주실 수 있습니까? –

+0

다음 행 :'var secondView = SecondViewController()'는'SecondViewController'의 인스턴스를 생성하고 있습니다. **이 ** 인스턴스에 연결되어 있습니까? 아니면 스토리 보드 단편을 통해'SecondViewController'에 도달 했습니까?그렇다면 ** 인스턴스 **에 대한 포인터를 만들어서'var secondView = SecondViewController()'를 피해야합니다. – Honey

+0

[이 답변보기] (https://stackoverflow.com/a/31934786/) 5175709) 및 링크 된 비디오 – Honey

답변

0

사실 내 원본 코드는 더 복잡합니다. Segues의 유무에 관계없이 정보를 전달하는 방법을보다 쉽게 ​​이해할 수 있도록 훨씬 간단한 연습 문제로 "문제 해결"을 시도했습니다.

두 ViewController를 Segue와 연결하고 있습니다 (그림 참조). SEGUE

  1. :

    Layout

    지금까지 내가 여기서 일 정보를 전달하는 두 가지 방법을 발견 SEGUE 두 ViewControllers를 연결하고 다음과 같은 예로서, (SEGUE에 대한) 준비 사용 제와의 ViewController 날기의 새로운 인스턴스를 생성하여 :

    //This piece of code goes inside the first ViewController 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
        if segue.identifier == "showSecondView" { 
         let secondView: SecondViewController = segue.destination as! SecondViewController 
         secondView.delegate = self 
        } 
    } 
    
  2. 인스턴스화 : 다음 프로토콜 함수를 사용

    //This piece of code goes inside the second ViewController 
    @IBAction func sendData(_ sender: Any) { 
        let text_to_pass = introducedText.text 
    
        let firstVC = storyboard?.instantiateViewController(withIdentifier: "GetData") as! ViewController 
        self.present(firstVC, animated: true) 
        firstVC.Label.text = text_to_pass 
    } 
    

두 옵션이 여기에 좋아,하지만 난 위의 코드는 작동하지 않는 이유를 이해하려면 : g는 정보를 원하시면 내가 예제 코드처럼, 두 번째보기 컨트롤러에서 필요합니다. 나는 많은 기사를 읽고 대부분의 사람들은 ViewDidLoad 내에서 secondView.delegate = self를 사용하고 있으며 매력처럼 작동합니다.

내가 뭘 잘못하고 있니?