简体   繁体   中英

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

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.

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在此处输入图片说明

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

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

单击根视图

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.

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 ( 在此处输入图片说明 ) 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.

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.

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.

此处允许函数之间的连接

此处不允许在函数中间进行连接

Note: I'm on Xcode 9.1

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

在此处输入图片说明

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.

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

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.

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.

确保有问题的项目已连接到您要控制将其拖动到的文件(在身份检查器中指定类)

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.

在尝试了所有其他答案之后,最终为我解决了这个问题的方法是重新启动 Xcode,我能够看到指示我可以放置操作方法的位置的蓝线...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM