2017-12-31 9 views
1

このように、ViewController変数を宣言する2つの異なる方法を試してみましたが、同じ結果が得られたようです。しかし、私はdestinationVC変数の設定に違いがなければならないと感じます。もしそうでなければ、新しいオブジェクトを宣言するより簡単な方法を使用しないでしょうか?ViewControllerオブジェクトを宣言する2つの方法の違い

[segue.destination as as segue.destination as! ViewControllerName] ViewControllerNameを用い

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "changeCityName" { 
     let destinationVC = segue.destination as! ChangeCityViewController 
     destinationVC.delegate = self 
    } 
} 

は、[()] prepareForSegue方法において

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "changeCityName" { 
     let destinationVC = ChangeCityViewController() 
     destinationVC.delegate = self 
    } 
} 
+4

コードの画像を投稿しないでください。写真を実際のコードに置き換えて(あなたの質問にコピーして貼り付けて)、あなたの質問を編集してください。 – rmaddy

答えて

0

は、新たなVCを作成するこれらの2つの方法が大きく異なります。

segue.destinationを使用する場合は、セグが予定されている特定のVC、つまりセグが接続されているストーリーボード内のVCを参照します。新しいVCを作成すると、作成したVCは、Segueと同じVCになりません。すなわち、あなたは別のVCを扱っています。新しく作成されたVCのデリゲートを設定しても、実際に提示されているVCには何も行われません。


あなたはVCとこれを提示するセグエを使用しての違いについて話している場合:

let vc = SomeViewController() 
self.present(vc, animated: true) 

その後、差が小さくなります。セグを使用すると、View Controllerのビューがストーリーボード(NIB)ファイルから読み込まれます。イニシャライザを呼び出してVCを作成する場合は、ビューコントローラクラスのビューの追加を処理する必要があります。

0

結果は視覚的には同じかもしれませんが、真ではありません。

あなたは(セグエのために)準備内部の任意のコードを配置していない場合はまだあなたは(視覚的に)

は(セグエのために)準備同じ結果が得られますUIViewControllersはストーリーボードを介して接続されたときに呼び出されます。

UIViewControllerはすでにストーリーボードに接続されているため、目的のUIViewControllerが目的のイベントで呼び出されます。

ご使用のでは、(segue.destination as!ViewControllerName)を使用して、segueを使用する正しい方法です。さらに、1つのより多くの事を行くことについて議論されるべきであり、それはその、すでにストーリーボード

1.Fromを介して接続する場合、我々は、コードを書くために必要とされるのはなぜ

は、内部(セグエのための)準備され

前にあなたの要件に応じていくつかのセグを接続することができますが、ボタンを押すたびに同じ準備(セグ)メソッドが呼び出されるので、呼び出すUIViewControllerを区別するためにこのようなことをします。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if(segue.identifier == "FirstViewControllerIdentifier") 
    { 

    }else if(segue.identifier == "SecondViewControllerIdentifier"){ 

    }else if(segue.identifier == "ThirdViewControllerIdentifier"){ 

    }else{ 

     // and so no 
    } 


} 

ここで、宛先コントローラ(UIViewController)のオブジェクトを既に準備しています。だから我々は

)の2.Weは、先のコントローラにデータを渡すことができ、また、我々はViewControllerNameを(使用して後者の場合で今デリゲート

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if(segue.identifier == "FirstViewControllerIdentifier") 
    { 
     // here we get object of first view controller and set delegate 
     let firstVC = segue.destination as! FirstViewController 
     firstVC.delegate = self 
    }else if(segue.identifier == "SecondViewControllerIdentifier"){ 
     // here we get object of second view controller and pass some data to it 
     let secondVC = segue.destination as! SecondViewController 
    secondVC.someData = someData 
    }else if(segue.identifier == "ThirdViewControllerIdentifier"){ 

    }else{ 

     // and so no 
    } 


} 

を設定することができ、宛先コントローラの新しいオブジェクトを作成する必要はありません(間違ったコード)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if(segue.identifier == "FirstViewControllerIdentifier") 
    { 
     // here we create object of first view controller.This object is different from the destination view controller 
     //you create an object and set a delegate but after that you are not using that object and that object is totallu useless. 
     let firstVC = FirstViewController() 
     firstVC.delegate = self 
     // above code does not affect any thing but the contoller which is to be presented is destination view controller which is connected through storyboard 
    } 


} 

あなたはセグエを使用して、任意の問題

があれば、私が知っているようにどのように理解してほしいです
関連する問題