5

I am creating an app that requires notifications. I created a settings page with a switch to toggle notifications for the application. I tried to link the switch to ViewController2 using an @IBAction, but it keeps connecting the object to Exit, instead of ViewController. I am getting really frustrated because the @IBAction won't stay connected. I am not sure what is going on, but if I could get help that would be amazing.

The images are linked down below to Imigur.

This is the connection to ViewController2

This is what occurs when I connect the IBAction to ViewController2

You can see there is no action option for the connection in this image.

  • I don't quite see what you mean by "there is no action option for the connection in this image". The Value Changed control event _is_ where you connect the action for the switch. – matt Aug 02 '18 at 00:12

3 Answers3

12

When you attach @IBActions (or @IBOutlets), you do not want to attach them to Exit on the View Controller.

Follow these steps to add an @IBAction:

1) Make sure your View Controller's class is linked to your file, then go into the Assistant Editor making sure you selected Automatic.

Using storyboards

2) Hold Control (^) and drag the button to your ViewController class.

Dragging item

3) Add the button as an @IBAction. - It's also recommended to use UIButton as the sender, instead of Any.

Connection action

It should look like this:

Code Send events


If you have questions, let me know!

George
  • 25,988
  • 10
  • 79
  • 133
7

The problem might be that you are supposing you can form an action connection from a UISwitch in the scene of one view controller to a different view controller. You can't. You can form outlets and actions only between a view controller and the interface inside the same scene of the storyboard.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I tried to add an action for a `UIBarButtonItem` in a `UINavigationBar` but it always said "Object: Exit" and also didn't let me create an outlet but google didn't give me any results describing the problem! Looks like I forgot to set the newly created ".swift" file as the class for the ViewController. Thanks! – Neph Jul 02 '19 at 14:36
  • @matt :) That was my issue ..LOL – Jack Jul 03 '21 at 13:25
0

Click on the top bar of your problematic ViewController (the overview of what is getting displayed on screen), click the top left icon (called View Controller).

On the right click "Show The Identity Inspector" icon and you should see class. Change the text to 'ViewController'. Hopefully it should auto fill.

This will solve the problem.

Sylvaus
  • 844
  • 6
  • 13
PHutch
  • 1
  • 2