25

I started working with xcode a few days ago, and today I tried to connect a textview from the main storyboard to the viewcontroller, but I figured out that its not working.

I am able to ctrl+click to drag the item, but unable to place it in both viewcontroller.h and .m. I double checked that its viewcontroller and not UIViewcontroller, so this is not the case.

I'm using a Yosemite 10.10 mac.

Any kind of help would be appreciated.

Ohad Klein
  • 369
  • 1
  • 3
  • 6

14 Answers14

32

For beginners -- > make sure you have kept the "Ctrl" key pressed down while dragging and dropping onto the code. This can be easily missed.

Abhijith C R
  • 1,440
  • 1
  • 12
  • 12
31

Select the ViewController in storyboard and change the name of the ViewController to the name in the .h or .m file and then try ctrl+click and drag enter image description here

Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
7

For me, the solution was to click on the "View" item in the dropdown menu that shows all the subviews for the controller.

Click on root view

Simply clicking on the controller image on the storyboard does not appear to properly "select" the controller for drag and drop abilities, the base view must be selected.

DiscDev
  • 38,652
  • 20
  • 117
  • 133
6

Make sure you're in Assistant Editor Mode (When code and layout editor are side-by-side).

From Apple:

With Interface Builder open in the standard editor pane, select the control you want to configure, and click the Assistant Editor button (enter image description here) in the workspace toolbar. The assistant editor opens your object’s implementation file.

Control-drag from the control in Interface Builder to the implementation file. (In the screenshot, the assistant editor displays the implementation file of the view controller for the Warrior button.) Xcode indicates where you can insert an action method in your code.

Alan
  • 1,510
  • 1
  • 18
  • 35
  • 1
    This is the correct solution! :) Some gotchas. Firstly, it opens the .m file by default. To counter this less than bright design, click Automatic in the breadcrumb path at the top of the pane. The dropdown will have 2 choices, .h and .m. After some fiddling you'll have set all of it up to perform the awesome drag operation. Secondly, don't drag from ring to ring: drag from the Inspector ring to the code line next to the ring. I counted 22 clicks and drag for something that could have been replaced with a single dropdown. Am I allowed to say Jesus Christ in here? Jesus Christ. – Henrik Erlandsson Feb 03 '17 at 14:08
  • In a sea of meaningless shapes, and inspectors that are probably minimised, would it be possible for someone to post a screenshot of what this looks like, with a big arrow pointing to where the Assistant Editor button is? – Graham Leggett Feb 22 '22 at 23:49
2

For anyone who is new to XCode this picture might help more:

Select the item in Main.storyboard from the right pane and then set the class for the view in the right pane. Now you can ctrl + drag into your class

enter image description here

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
2

After trying all of the other answers, what finally fixed this issue for me is simply restarting Xcode and I was able to see the blue line that indicates where I can place my action method...

1

Try selecting your view in the storyboard and clicking on the identity inspector on the right side in class. Enter your view controller name and then drag and drop the item from the storyboard to the view controller.

Hope it helps.

shim
  • 9,289
  • 12
  • 69
  • 108
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
1

Note that dragging to any point in your code doesn't necessarily work. If you've been unable to drag to create the connection in one spot of your code, try dragging to another spot. For me, I was trying to drag to add a connection in the middle of a function which didn't work. Dragging to the beginning line of a function or in between functions did work.

Connection was allowed here between functions

Connection was not allowed here inside the middle of a function

Note: I'm on Xcode 9.1

Ian D. Miller
  • 313
  • 2
  • 9
  • I am unable to find this multiple window option in XCode 11 any idea? – Manish Pathak Dec 28 '19 at 07:59
  • In my case, I was trying to -drag into a file that had an UN-RESOLVED syntax error (in fact it was an incomplete edit), which caused XCode to reject drags even when it was in-between functions. Completing the edit fixed the problem. – gOnZo Feb 21 '20 at 16:54
1

I had to both rename the class (from ViewController to ViewController2) of the ViewController.swift file and change the Custom Class (from ViewController to ViewController2) of the Main Storyboard's View Controller to make Ctrl-Drag-and-drop work again.

Renaming back to ViewController from ViewController 2 in ViewController.swift and Main Storyboard did not thwart the Ctrl-Drag-and-drop, it remained working afterward as well.

0

Hey I think you are not connected your storyboard viewController with your viewController class. check screen shot which "suhit" uploaded in his answer. I would like to tell you something more than it. When ever you need to connect any storyboard object with class.

  • first go to utility panel (right side) and then set class there.
  • after that you can connect your controls with class

I hope it will help you.

Pravin Tate
  • 1,145
  • 8
  • 18
0

Check that the names on Viewcontroller identity inspector and the .h, .m files are the same. That worked for me

nano_dorado
  • 89
  • 2
  • 2
  • 10
0

I'm using Xcode 9.2 I had my ViewControlled refactored into sections using the "extension" keyword. It appears you can't control drag into an extension of a ViewController. If you go up to the top part that is in the class definition, control dragging works fine (assuming you did the other things in this thread about making sure the right class is associated with the view controller.

D. Rothschild
  • 659
  • 9
  • 14
0

I had same problem, I realised I selected wrong story board. If you made app with default settings you will get two story boards (Main and Launch Screen). Labels on Launch screen story board cannot drag drop.

bf39L
  • 149
  • 2
  • 14
0

Make sure that the item in question is connected to the file in which you want to control drag it to (specify the class in the identity inspector)

Shawn
  • 9
  • 1