5 Replies
      Latest reply on Jun 13, 2019 4:59 PM by QuinceyMorris
      bbabbitt46 Level 1 Level 1 (0 points)

        I am using Swift 3 with Xcode 8.2 (I can't afford a new Mac yet). I created a second ViewController by pulling it from the object library on to my storyboard. I named it ValuesView. I added a button in the parent to segue to ValuesView. As the name implies, I wanted to display values I obtained elsewhere (not relavent where). If I add text fields, I can't send data to them because I can't find their code! I can't even add an Outlet or Action for a button. Where is the code for this ViewController and how can I get to it (e.g. add buttons and textFields, etc.)?

        • Re: 2nd ViewController
          QuinceyMorris Level 8 Level 8 (6,040 points)

          You need to create a second UIViewController subclass for the second view controller. In the source file for that subclass, you can add the outlets specific to that controller.

           

          To get your data into the new view controller, if it knows where to find it, there's no real problem. (You'll most likely get in in a viewDidLoad override.) However, if you need to "inject" (i.e. "send") the data into the second view controller, you can get a reference to it in a prepareForSegue method in the first view controller, and set the data into a property of the second view controller just before the segue runs.

          • Re: 2nd ViewController
            Claude31 Level 8 Level 8 (6,515 points)

            The simplest way to do this is to create the subclass in code and Interface builder at once.

             

            - In File, select New… then file

            - In the panel, select IOS, then Cocoa Touch, click next

            - In the following panel, select subclass of: UIViewController (second line)

            - Give a meaningfull name in firstsuch as secondViewController

            - Most important, select the checkbox "also create xib file"

             

            You will get 2 files in your project, already connected.

              • Re: 2nd ViewController
                bbabbitt46 Level 1 Level 1 (0 points)

                Thanks for your help. This is my first venture into iOS programming. I have used many IDEs for quite a few compilers and builders, from simple C code compilers to NetBeans and Microcrap's Visual Studio. Xcode and Swift are some strange critters. I built a really nice (and somewhat complex) second visualController, named ValuesView in Storyboard that shows everything I need to display my data. Since I can't find the code for that visual Controller, is there any way I can make is a subclass of the first visual Controller without losing the items and placement of what I had built? If that isn't possible, can I rebuild ValuesView with Storyboard? How would I do that?

                  • Re: 2nd ViewController
                    QuinceyMorris Level 8 Level 8 (6,040 points)

                    I think you're missing one piece of the puzzle here, but I'm not sure which.

                     

                    Each "scene" in your storyboard has a corresponding view controller class, which may be UIViewController or a custom subclass. If it's a subclass, you should create a "<yourChosenName>ViewController.swift" source file that defines the subclass.

                     

                    Then, in the storyboard canvas, show the Identity inspector over on the right, and type (or choose from the popup) the name of your subclass. (When you press Return, Xcode should check the "Inherit module from target" box, which is correct.) This class is important, because it controls what class of object is created when the scene is instantiated. If you set your second view controller's class name on its storyboard scene, you will get the right object at run time.

                     

                    In addition, you can create @IBOutlet properties in your view controller's .swift source file, then you can make connections in the storyboard scene, to set the run-time values for those properties. In addition, for controls that have actions, you can create @IBAction methods in the source file, and connect the action target to the view controller in the storyboard.

                     

                    There's nothing actually stopping you from designing a new scene in the storyboard with the "wrong" view controller class, but it won't work at run-time. If you simply define the second view controller in source, then change the scene's view controller's class name in the Identity inspector, I think you'll end up with what you want.