Can't drag and drop item from storyboard to viewcontroller in xcode
Asked Answered
N

14

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.

Number answered 16/7, 2015 at 12:50 Comment(0)
L
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.

Lentissimo answered 3/10, 2017 at 7:23 Comment(3)
This helped me!Religieux
Thanks it is really helpedCurbing
Thank you so much, very short answer yet so helpful!Catheterize
F
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

Frisbee answered 16/7, 2015 at 12:59 Comment(5)
If this still doesn't work, you might have to reboot xcode once the class is named (that's what I had to do)Candlefish
If you do this and it still doesn't work, check the Attributes inspector and make sure the 'User Interaction Enabled' button is selected. This is unselected by default for some classes.Afterheat
Thanks. I think apple can make a dialog to give drag and drop a hint.Schema
I think it’s covered in the documentation somewhere but I agree they should highlight itFrisbee
Solved it for me, why isn't this marked as the answer? @Ohad KleinPumping
B
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.

Banka answered 1/12, 2015 at 2:35 Comment(0)
C
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.

Currey answered 29/9, 2016 at 19:31 Comment(2)
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.Surveying
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?Proffer
S
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

Sedation answered 8/1, 2021 at 22:33 Comment(0)
G
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...

Gorga answered 26/6, 2021 at 16:48 Comment(1)
This was true for me in Xcode 13.2.1. I was connecting between a View in Interface Builder and a NSViewController in a .swift file.Quenby
F
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.

Fibula answered 16/7, 2015 at 12:56 Comment(0)
N
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

Nicholas answered 15/11, 2017 at 17:18 Comment(2)
I am unable to find this multiple window option in XCode 11 any idea?Chanson
In my case, I was trying to <ctrl>-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.Potts
T
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.

Toulouselautrec answered 8/8, 2019 at 7:26 Comment(0)
T
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.

Teahouse answered 16/7, 2015 at 13:20 Comment(0)
E
0

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

Ezraezri answered 20/10, 2016 at 23:5 Comment(0)
S
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.

Schaefer answered 20/2, 2018 at 23:11 Comment(0)
E
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.

Elodea answered 17/7, 2018 at 4:31 Comment(0)
D
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)

Damalas answered 5/11, 2018 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.