2016-11-01 8 views
0

私はアプリを開発しており、同時に迅速に学習しています。私はsecondViewControllerからfirstViewControllerにデータを送る方法を考え出しました。基本的には、 "done"ボタンをfirstViewControllerにリンクしています。そのため、 "完了"を押すと、最初のviewControllerの新しいウィンドウが開き、渡されたデータが表示されます。Swift:新しいウィンドウでviewControllerを開かずにpreviousViewControllerにデータを渡す

しかし、私は最初のViewControllerを更新する方法を探していますが、その別のウィンドウを別のセグで開かなくても更新できます。

これを説明する最善の方法は、私のfirstViewControllerには、secondViewControllerを開く「追加」ボタンがあります。このviewControllerには2つのテキストフィールドがあります。最初のviewControllerに表示されるように、これらの2つのテキストボックスに書かれたものが必要です。& doneを押すと、secondViewControllerが終了し、firstViewControllerに戻ります。

これは私が使用していたコードです: (secondViewController中)

override func prepare(for segue: NSStoryboardSegue, sender: Any?) { 
var destinationviewcontroller : ViewController = segue.destinationController as! ViewController 
    destinationviewcontroller.LabelText = weblinklabel.stringValue 
    } 

明らかlabeltextはsecondViewControllerのもののテキストフィールドに書かれているものを示してfirstviewcontroller内のラベルです。

問題はストーリーボードを使用して「完了」ボタンからfirstViewControllerにドラッグすると、firstViewControllerが新しいデータで新しいウィンドウに開きます/ labeltextです。 "done"と更新されたラベルが新しいウィンドウで開かれずにfirstViewControllerに表示されると、2番目のViewControllerを閉じるだけです。

説明が少し難しいので、明確にするために詳細が必要な場合は、私はそれを与えることを喜んでします!

ありがとうございます!

+1

サウンズ(http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and -how-do-you-use-them#12843906) – PeejWeej

答えて

0

はい、unwind Segues as stated hereを使用する必要があります。

まず、あなたのfirstViewControllerに空IBActionメソッドを追加します(あなたが直接それを書く必要があります注意してください):

@IBAction func unwindToFirstViewController (sender: UIStoryboardSegue) { 
} 

が一度書かれている、今、あなたのストーリーボードにあなたにExit項目にごDoneボタンをControl+dragすることができます

enter image description here

それはワットという新しいIBAction方法をポップする必要があります。このようsecondViewController、病気今ちょうどそれを実装し、アンワインドを扱う:あなたは[Seguesをほどく]使用する必要がありますように

@IBAction func unwindToFirstViewController (sender: UIStoryboardSegue) { 
    if let sourceViewController = sender.source as? SecondViewController { 
     // update whatever values you want 
     // to update in firstViewController from secondViewController 
    } 
} 
+0

iOSアプリケーションではなく、ココアアプリケーションを作っていると思うので、私はストーリーボードに「Exit」アイテムを持っていません。あなたが言及しているunwindsegueメソッドは、iOSアプリケーションのためだと思います。 –

関連する問題