Prepare doesn't get called on segue (Swift 4.2)












0















I've searched for an answer here, but wasn't able to find the exact same issue I have, please help me so sort it out.



The problem is that when I create a separate view and try to make a segue using storyboard and navigation view controller, the segue itself works fine, but



override func prepare(for segue: UIStoryboardSegue, sender: Any?)



does not get called. To make my example as minimal as possible I've make a toy app to show what's going on.



That's how the overall storyboard schema looks like. ViewController is embedded in NavigationController.



Segue is made with ctrl + SecondViewController, identifier is set.



Custom class for SecondViewController is set.



View controllers have the following implementation:

Main view controller sets the title of the only button:



class ViewController: UIViewController {

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

button.setTitle("Awesome title", for: .normal)
}
}


On button press the label is meant to be updated from the button's title:



class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
print("SecondViewController viewDidLoad method")
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(print("SecondViewController prepare method"))
if let text = (sender as? UIButton)?.titleLabel?.text {
label.text = text
}
}
}


I intentionally put two print statements to show that viewDidLoad actually gets called, but not prepare.



Here is a gif showing how the app works. As you may see label on the second view doesn't get updated either.



I've also tried to set segue this way, then create an @IBAction function and manually call performSegue:



@IBAction func onButton(_ sender: UIButton) {
performSegue(withIdentifier: "Id", sender: self)
}


Please help me to understand and solve the problem. Thank you.










share|improve this question



























    0















    I've searched for an answer here, but wasn't able to find the exact same issue I have, please help me so sort it out.



    The problem is that when I create a separate view and try to make a segue using storyboard and navigation view controller, the segue itself works fine, but



    override func prepare(for segue: UIStoryboardSegue, sender: Any?)



    does not get called. To make my example as minimal as possible I've make a toy app to show what's going on.



    That's how the overall storyboard schema looks like. ViewController is embedded in NavigationController.



    Segue is made with ctrl + SecondViewController, identifier is set.



    Custom class for SecondViewController is set.



    View controllers have the following implementation:

    Main view controller sets the title of the only button:



    class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
    super.viewDidLoad()

    button.setTitle("Awesome title", for: .normal)
    }
    }


    On button press the label is meant to be updated from the button's title:



    class SecondViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()
    print("SecondViewController viewDidLoad method")
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    print(print("SecondViewController prepare method"))
    if let text = (sender as? UIButton)?.titleLabel?.text {
    label.text = text
    }
    }
    }


    I intentionally put two print statements to show that viewDidLoad actually gets called, but not prepare.



    Here is a gif showing how the app works. As you may see label on the second view doesn't get updated either.



    I've also tried to set segue this way, then create an @IBAction function and manually call performSegue:



    @IBAction func onButton(_ sender: UIButton) {
    performSegue(withIdentifier: "Id", sender: self)
    }


    Please help me to understand and solve the problem. Thank you.










    share|improve this question

























      0












      0








      0


      1






      I've searched for an answer here, but wasn't able to find the exact same issue I have, please help me so sort it out.



      The problem is that when I create a separate view and try to make a segue using storyboard and navigation view controller, the segue itself works fine, but



      override func prepare(for segue: UIStoryboardSegue, sender: Any?)



      does not get called. To make my example as minimal as possible I've make a toy app to show what's going on.



      That's how the overall storyboard schema looks like. ViewController is embedded in NavigationController.



      Segue is made with ctrl + SecondViewController, identifier is set.



      Custom class for SecondViewController is set.



      View controllers have the following implementation:

      Main view controller sets the title of the only button:



      class ViewController: UIViewController {

      @IBOutlet weak var button: UIButton!

      override func viewDidLoad() {
      super.viewDidLoad()

      button.setTitle("Awesome title", for: .normal)
      }
      }


      On button press the label is meant to be updated from the button's title:



      class SecondViewController: UIViewController {
      @IBOutlet weak var label: UILabel!

      override func viewDidLoad() {
      super.viewDidLoad()
      print("SecondViewController viewDidLoad method")
      }

      // MARK: - Navigation

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      print(print("SecondViewController prepare method"))
      if let text = (sender as? UIButton)?.titleLabel?.text {
      label.text = text
      }
      }
      }


      I intentionally put two print statements to show that viewDidLoad actually gets called, but not prepare.



      Here is a gif showing how the app works. As you may see label on the second view doesn't get updated either.



      I've also tried to set segue this way, then create an @IBAction function and manually call performSegue:



      @IBAction func onButton(_ sender: UIButton) {
      performSegue(withIdentifier: "Id", sender: self)
      }


      Please help me to understand and solve the problem. Thank you.










      share|improve this question














      I've searched for an answer here, but wasn't able to find the exact same issue I have, please help me so sort it out.



      The problem is that when I create a separate view and try to make a segue using storyboard and navigation view controller, the segue itself works fine, but



      override func prepare(for segue: UIStoryboardSegue, sender: Any?)



      does not get called. To make my example as minimal as possible I've make a toy app to show what's going on.



      That's how the overall storyboard schema looks like. ViewController is embedded in NavigationController.



      Segue is made with ctrl + SecondViewController, identifier is set.



      Custom class for SecondViewController is set.



      View controllers have the following implementation:

      Main view controller sets the title of the only button:



      class ViewController: UIViewController {

      @IBOutlet weak var button: UIButton!

      override func viewDidLoad() {
      super.viewDidLoad()

      button.setTitle("Awesome title", for: .normal)
      }
      }


      On button press the label is meant to be updated from the button's title:



      class SecondViewController: UIViewController {
      @IBOutlet weak var label: UILabel!

      override func viewDidLoad() {
      super.viewDidLoad()
      print("SecondViewController viewDidLoad method")
      }

      // MARK: - Navigation

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      print(print("SecondViewController prepare method"))
      if let text = (sender as? UIButton)?.titleLabel?.text {
      label.text = text
      }
      }
      }


      I intentionally put two print statements to show that viewDidLoad actually gets called, but not prepare.



      Here is a gif showing how the app works. As you may see label on the second view doesn't get updated either.



      I've also tried to set segue this way, then create an @IBAction function and manually call performSegue:



      @IBAction func onButton(_ sender: UIButton) {
      performSegue(withIdentifier: "Id", sender: self)
      }


      Please help me to understand and solve the problem. Thank you.







      navigation segue xcode10 swift4.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 18:34









      snk4trsnk4tr

      14




      14
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Ok, I figured out what's wrong. For those who still feel confused by the way segues work in IOS have a look at the diagram below:



          Segues in IOS (taken from here)



          Note that prepare (for: sender) is located in View A, not in View B. This is critical since it gets triggered before the actual segue is performed. Moreover, if you assign some values to View B parameter (for instance UIIMageView), then the view gets rendered considering these changes.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997947%2fprepare-doesnt-get-called-on-segue-swift-4-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Ok, I figured out what's wrong. For those who still feel confused by the way segues work in IOS have a look at the diagram below:



            Segues in IOS (taken from here)



            Note that prepare (for: sender) is located in View A, not in View B. This is critical since it gets triggered before the actual segue is performed. Moreover, if you assign some values to View B parameter (for instance UIIMageView), then the view gets rendered considering these changes.






            share|improve this answer




























              0














              Ok, I figured out what's wrong. For those who still feel confused by the way segues work in IOS have a look at the diagram below:



              Segues in IOS (taken from here)



              Note that prepare (for: sender) is located in View A, not in View B. This is critical since it gets triggered before the actual segue is performed. Moreover, if you assign some values to View B parameter (for instance UIIMageView), then the view gets rendered considering these changes.






              share|improve this answer


























                0












                0








                0







                Ok, I figured out what's wrong. For those who still feel confused by the way segues work in IOS have a look at the diagram below:



                Segues in IOS (taken from here)



                Note that prepare (for: sender) is located in View A, not in View B. This is critical since it gets triggered before the actual segue is performed. Moreover, if you assign some values to View B parameter (for instance UIIMageView), then the view gets rendered considering these changes.






                share|improve this answer













                Ok, I figured out what's wrong. For those who still feel confused by the way segues work in IOS have a look at the diagram below:



                Segues in IOS (taken from here)



                Note that prepare (for: sender) is located in View A, not in View B. This is critical since it gets triggered before the actual segue is performed. Moreover, if you assign some values to View B parameter (for instance UIIMageView), then the view gets rendered considering these changes.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 15 at 18:33









                snk4trsnk4tr

                14




                14
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997947%2fprepare-doesnt-get-called-on-segue-swift-4-2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith