0

I am creating an app with multiple view controllers (I believe this is the best way to have multiple screens, correct me if I am wrong). When I connect a text field from my main ViewController it works fine and I get the following code.

@IBOutlet weak var typeField : UITextField!

However, when I try to connect a text field from a new ViewController I get the wrong code and cannot change the options to correct it. This is what I get.

@IBAction func typeField(_ sender: Any) {
}

I really do not know how to fix this, can anyone please help.

shodai
  • 25
  • 4

1 Answers1

0

When you right click and drag from the TextField onto your code to create the connection, it gives you an option when the box appears after you release the mouse button to select action or outlet, if you don't choose sometimes it will be action and sometimes it will be outlet. You have to select which with the arrows in the "Connection" box. This box is shown in the photo. Actions will create a method with a built in handler for you to use to execute code like when you click a button which the action is assigned to. An outlet will be for if you want to control aspects of the particular component itself, such as getting or setting text or changing image.

enter image description here

wdbwdb1
  • 195
  • 2
  • 9
  • That works for the main view controller, but for the others it only allows for action. I tried to attach photos but I can't seem to figure out how to show it. – shodai Aug 09 '20 at 13:08
  • Could the problem be the way that I am connecting the View Controllers? I using option+click to drag from a button on the home view controller to attach to the next view controller; to change screens. There are a series of options that appear and I select "Show". Should I be doing something different? – shodai Aug 09 '20 at 16:15
  • So you have 2 ViewControllers, do you have 2 Swift classes (your Swift files you are writing code on) and are they assigned to their respective view controllers like in this answer? https://stackoverflow.com/questions/48085341/text-field-creates-action-not-outlet/48085563 – wdbwdb1 Aug 09 '20 at 18:22
  • Enter the name of the Swift class file to be assigned to the ViewController in the Identity Inspector, click on the yellow circle on the top of the view controller in your storyboard and then you can enter the class name in the Identity Inspector tab in the right panel. Make sure you don't have any extra connections for outlets or actions that maybe you created as a mistake and deleted the code to, the connections are still in place even if the code is deleted, and these can cause errors. Here's a video showing how to remove them: https://www.youtube.com/watch?v=HC5XwXpXeP0 – wdbwdb1 Aug 09 '20 at 18:22
  • I use the right mouse button to click and drag to create connections, and yes "Show" is usually the best choice for a segue, this explains all the options: https://stackoverflow.com/questions/25966215/whats-the-difference-between-all-the-selection-segues – wdbwdb1 Aug 09 '20 at 19:12
  • Alright, so I was able to create a new file with a new class and added the class within the storyboard, then realized that I needed to add the module and storyboard ID, and finally it works. Thanks everyone for the direction. – shodai Aug 09 '20 at 21:13