2017-12-24 7 views
0

私は2番目のビューコントローラどのようにビューコントローラとTableViewController間のデータを渡すperformSegue swift 4で開いたページを閉じるを使用して?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let vc = segue.destination as? secondViewController { 
     vc.showPageType = self.checkEdit 
    } 

に最初のビューコントローラからデータを渡すためにここにこのコードを使用ししかし、問題は、第2のビューコントローラで、私は、ユーザーがそのテキストフィールドを記入し、押していることをテキストフィールドを持っているということですボタンはsecondViewControllerは、この方法で

dismiss(animated: false, completion: nil) 

を解任され提出し、今私はswift4でそれを行うことができますどのように最初のビューコントローラにテキストフィールドのテキストを渡すためにセグエメソッドを実行する使用することはできませんか?

+1

:あなたの第二のコントローラでちょうど解任呼び出す前に(アニメーション:偽、完了:ゼロ)提示する前に、第二のコントローラのpertyデリゲートを使用してB-> Aからデータを渡します。 –

+1

[ViewControllerを却下して別のコントローラにデータを渡すにはどうすればいいですか?](https://stackoverflow.com/questions/43706662/how-to-pass-data-to-another-controller-on-dismiss-viewcontroller) –

+0

問題があります。私のfirstviewcontrollerはtableviewcontrollerで、ビューコントローラではありません。 –

答えて

0

あなたsecondViewControllerのソースコードファイルに追加します。

protocol SecondViewControllerDelegate { 

    func submitButtonPushedWithText(_ text: String) 
} 

をクラスsecondViewControllerプロパティに追加します。

var delegate: SecondViewControllerDelegate? 

その後SecondViewControllerDelegateにあなたの最初のコントローラに準拠し、方法submitButtonPushedWithText(:)を実装:

class FirstViewController: UIViewController, SecondViewControllerDelegate { 

    func submitButtonPushedWithText(_ text: String) { 
     // use text from textField of second controller 
    } 
} 

また、

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
if let vc = segue.destination as? secondViewController { 
    vc.showPageType = self.checkEdit 
    // setup delegate 
    vc.delegate = self 
} 

を今、あなたはメソッドsubmitButtonPushedWithTextを呼び出す(_テキスト:String)をすることができます

func submitButtonPushed() { 
    delegate?.submitButtonPushedWithText(textField.text!) 
    dismiss(animated: false, completion: nil) 
} 
関連する問題