Chapter 4 : Can't drag the Outlet of the slider to View Controller

Hi everyone,

I am a beginner and I have a problem in my code : I can’t drag the outlet of the slider to view controller (I can’t select it). I checked my code and it seems good to me but I think I missed something. Here are the picture of my code. Thanks for the help.

I am afraid the screenshot of the code doesn’t help much since that only tells us that the code is crashing because you don’t have the outlet connected. And we already know that the outlet is not connected :slight_smile:

When you say you can’t select “it” - what is the “it” there? Can’t you select the slider in your storyboard? Or something else?

Have you tried selecting the view controller in your storyboard, switching to the Connections inspector and then dragging from the outlet for the slider to the slider on the storyboard?

You need to Ctrl+drag in the other direction. To connect an action/method in a ViewController to a control in the storyboard, you Ctrl+drag from the control in the storyboard to the ViewController.

On the other hand, to connect an outlet in the ViewController to a control in the storyboard, you Ctrl+drag from the ViewController to the control in the storyboard.

Thank you both for your answers !

Unfortunately, it didn’t work. Here is what I meant when I said " I can’t drag it the Slider to the View Controller ".

I also tried to drag it in the other direction (from the View Controller to the Slider in the storyboard, as you can see it in the screenshot.

And I don’t know what I did but now when I build the current code, I only have a white screen, I may need to start it again :sweat_smile:

It looks as if you don’t have a custom view controller class set for your view controller in your storyboard. You probably accidentally cleared the Class value in the Identity inspector. Check the Identity inspector to see what you have set as the Class and fix it if it is empty or has something besides ViewController.

If you had the correct class set, in the Connections inspector, you should see the outlets you defined in your code. Since you can’t see the outlets, it’s an indicator that you have the wrong class set for your view controller.

Also, if you look closely, you’ll notice that the two actions that you have already hooked up have exclamation points next to them - indicating that they no longer exist either. So the most probable scenario is that your view controller class property was accidentally changed …

Thank you for your answer.

When I click on the View Controller, my Custom Class is ViewController and when I click in the storyboard, I changed my Custom Class to SKView (there was nothing inside before).

I removed the line currentValue = lroundf(slider.value) and it worked again. But I tried to fix the two actions hooked up having exclamation points, so I removed then and tried to drag them again to the View Controller but it didn,'t work, I don’t have any " Sent Events " when I drag my button to the View Controller.

I will restart the app from scratch and try to see what changes or what I missed. Thank you for your help !

I am not quite sure what you mean by the above. Your custom class should not be SKView - that is wrong. It should be ViewController. Can you please provide a screenshot showing the current settings for your view controller in the Identity inspector?

I can’t drag from (or to) new referencing outlet in that popup either. I’ve never tried to set an outlet like that. Instead, I drag from the yellow icon at the top of the ViewController in the storyboard to the control.

Hold on. In the same popup as in your image, I have a section at the top called Outlets, which lists all the variables declared as an @IBOutlet in the ViewController, and I can drag from my slider outlet variable to the control to create the connection. But your popup menu does not have the Outlet section.

I changed my Custom Class to SKView (there was nothing inside before).

What evil spirits possessed you to do something like that?!

When I click on the View Controller, my custom class is ViewController. But when I click in the storyboard on a part where there is no Button, Label or Slider, my custom class changes and as I thought it was SKView, I changed it to SKView :sweat_smile:

I just saw your pictures 7stud, and it’s strange because I don’t any section called Outlets. But let me start the app again and try to see what went wrong, and I will tell you more about it.

In any case, thank you both for your answers, I really appreciate it, that’s a great forum

Hi guys. I restart the app from the beginning and I was finally able to declare the variable as an @IBOutlet but unfortunately I still have a message next to the line currentValue = lroundf(slider.value) saying Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

(I compare my image with yours 7stud and I found out that next to your view Outlet, there is only a view, whereas mine there is Horizontal Slider + View)

Okay, sorry guys, just realised I draged it to View and not Slider, my bad. It’s working !

By default, the type of the view is UIView. Nowhere in the book did it tell us to change the class of the view to SKView in the Identity Inspector. I tried changing my view’s class to SKView then running the app, and I got a weird error that said to check to make sure I had permission to run the app, and I couldn’t get rid of that error, so I had to quit the Simulator and Xcode. In any case, you can change the class of the view back to UIView. Somewhat surprisingly there is no choice for UIView in the drop down list, but you can type UIView in by hand. Hopefully, you didn’t delete the app, and you started a second one.

I’ve noticed that the view outlet is different from the other outlets. If you accidentally connect the view outlet to something else, the Connections Inspector still shows the View (a UIView) that the outlet was originally connected to grayed out underneath the new control that the view outlet is connected to. And if you break the other connection, the view outlet will automatically reconnect to the View. So, Xcode requires that the view outlet be connected to something.

In any case, other people have made the same mistake and connected their view outlet to some control. I always check the storyboard’s table of contents to make sure that I’ve selected the correct control in the storyboard.

Hi.

Everything was working, I correctly added the slider Outlet and checked it on the Connections Inspector, everything was fine. I closed the app, and then reopen it later to work on the startNewRound part.

When I had to drag the new referencing Outlet of Label 100 to the View Controller Icon above, I had the same problem as before, I can’t select it. And then I checked the Connections Inspector and those same exclamation point has appeared again besides each Sent Actions and the slider Outlet, eventhough when I click on View Controller, my customclass is indeed ViewController. When I launch the app, everything is working the same as before. I searched for the problem but couldn’t find any solution. Not sure what I should show to you as a screenshot.

Can you please upload your full project as a ZIP file somewhere and provide a link to it? Not sure what is going on here and would like to see the project myself since this appears to be a recurring issue for you.

Here is a link to my uploaded projet : https://ufile.io/vyhyt

I checked the Xcode project you provided and had no issues selecting the 100 label or setting up the connection at all. I’ve attached a video showing setting up the outlet using your project. Do note that when you drag from the yellow circle for the view controller, you do need to hold down the CTRL (Control) key. If you drag from the Connections inspector, you do not have to do this …

There are multiple ways to set up an outlet though and the book might switch between different ones to show you all the possibilities.

Here’s the video (as a ZIP file):

xcode.mp4.zip (589.0 KB)

I tried it and still can’t set up the connection, it’s strange. Here is a link to the video : https://ufile.io/d4cjy

Do you also have those same exclamation points in the Connections Inspector when you run the project ?

Do you also have those same exclamation points

I can get those exclamation marks to show up in the Connections Inspector–if I open the file ViewController.swift and change the class name to:

class MyViewController: UIViewController {

After doing that, if I go to the storyboard and select View Controller in the storyboard’s table of contents, and then I open the Identity Inspector, and I look at the top section in the Identity Inspector, which is titled Custom Class, next to Class is the name ViewController–which does not match the new name of my class:

class MyViewController: UIViewController {

I also experience the exact same dragging problems in the storyboard that you do. Did you somehow misspell “class ViewController” in ViewController.swift or in the Identity Inspector?

I also notice that when I change my class name in ViewController.swift to:

class MyViewController: UIViewController {

the circles next to my declared outlets instantly change from filled in to blank, and when I change the class name back to:

class ViewController: UIViewController {

all the circles get filled in again–and the exclamation marks disappear in the Connections Inspector.

My tests show that as long as the class name in ViewController.swift matches the class name listed in the Identity Inspector in the storyboard, then the app runs fine. In other words, the class name in ViewController.swift does not have to match the file name.

Also, when you change the class name in the Identity Inspector be sure to Tab out of that field or else the class name won’t take effect.

I did not try running the project but everything was fine with the outlets when I checked. Based on your video, it looks very much like an Xcode issue at your end. Here’s what you can try (if you haven’t already):

  1. Clean your project and see if that helps. You can do this from the Product menu item on the main menu.

  2. Quit Xcode and restart.

  3. Reboot your machine.

Generally, the last option fixes most Xcode issues. Let me know if none of these help and I’ll see if there’s something else I can think of which might help.

Hi, I tried these three options but the problem is still here. I guess it is related to the exclamation points in the Connections Inspector.

Do you think that I might have done something wrong ? It’s strange because it has happened to me before with the Slider Outlet too.