2

When I ctrl-drag from a UIButton in storyboard to the viewcontroller class, Xcode is not showing the option to create an IBAction.

I have already tried to start a new project and it still does not have the Connect: Action option available.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
  • 1
    Make sure that the class to which you are trying to drag the outlet is the View's controller – Jobins John Sep 04 '17 at 12:26
  • What @JobinsJohn asks you to check is correct. Also, I've seen this behavior *even when* the correct VC is set in IB. While I honestly do not know why it happens, I've seen two things: First, what *does* the code view have for code? Please, if the VC is set correctly show us this. Second, try closing/reopening Xcode. For me, that clears things up. –  Sep 04 '17 at 14:17

3 Answers3

1

Please follow below steps :-

  1. Open storyboard and yourViewController side by side after clicking on Assistant Editor.

enter image description here

  1. Now Drag and Drop from UIButton to class file. It will open one popup menu.

enter image description here

  1. Now click on connection it will open option menu with Outlet, Action, Outlet Connection.

enter image description here

  1. Now click on Action. It will set within the same popup menu, and ask for method name. Here you have to add your Method Name, and click on connect button.

enter image description here

  1. At the last point you will find your method within your class, with @IBAction connection.

enter image description here

Hope above steps is useful for you.

Shah Nilay
  • 778
  • 3
  • 21
  • The step 3 is not displaying Acionfor me. That is the problem –  Sep 04 '17 at 12:41
  • Confirm that storyboard class which contains `UIButton` and in assistant editor is the same class or different? Inshort check your storyboard class and .swift class. Both are same or not. – Shah Nilay Sep 04 '17 at 12:48
0

Check that the ViewController cocoa class is correctly connected to the ViewController in the Interface builder, then,try with this answer: https://stackoverflow.com/a/37322981/8038084.

0

Sometimes, even restarting and cleaning does not help me. What I do is make it the other way round.

Create an IBAction manually in your code.

@IBAction func buttonPressed() {
    [...]
}

The function should appear inside your IB under Connections inspector/Received Actions...

Connections inspector/Received Actions

where you can simply connect it to your button/gesture recognizer etc.

list of control events

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223