简体   繁体   English

无法将项目从故事板拖放到 xcode 中的视图控制器

[英]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.几天前我开始使用 xcode,今天我尝试将一个 textview 从主故事板连接到 viewcontroller,但我发现它不起作用。

I am able to ctrl+click to drag the item, but unable to place it in both viewcontroller.h and .m.我可以按 ctrl+click 拖动该项目,但无法将其同时放置在 viewcontroller.h 和 .m 中。 I double checked that its viewcontroller and not UIViewcontroller, so this is not the case.我仔细检查了它的 viewcontroller 而不是 UIViewcontroller,所以情况并非如此。

I'm using a Yosemite 10.10 mac.我使用的是优胜美地 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选择storyboard中的ViewController,将ViewController的名字改为.h或者.m文件中的名字,然后尝试ctrl+click并拖动在此处输入图片说明

For beginners -- > make sure you have kept the "Ctrl" key pressed down while dragging and dropping onto the code.对于初学者 --> 确保在拖放到代码上一直按下“Ctrl”键 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 (在标准编辑器窗格中打开 Interface Builder 后,选择要配置的控件,然后单击 Assistant Editor 按钮 ( 在此处输入图片说明 ) 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.从 Interface Builder 中的控件按住 Control 拖动到实现文件。 (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. (在屏幕截图中,助手编辑器显示了 Warrior 按钮的视图控制器的实现文件。)Xcode 指示您可以在代码中插入操作方法的位置。

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注意:我使用的是 Xcode 9.1

For anyone who is new to XCode this picture might help more:对于不熟悉 XCode 的任何人,这张图片可能会有所帮助:

Select the item in Main.storyboard from the right pane and then set the class for the view in the right pane.从右侧窗格中选择Main.storyboard的项目,然后在右侧窗格中设置视图的类。 Now you can ctrl + drag into your class现在您可以按ctrl + drag到您的班级

在此处输入图片说明

Hey I think you are not connected your storyboard viewController with your viewController class.嘿,我认为您没有将您的故事板 viewController 与您的 viewController 类联系起来。 check screen shot which "suhit" uploaded in his answer.检查“suhit”在他的回答中上传的屏幕截图。 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.检查 Viewcontroller 身份检查器和 .h、.m 文件上的名称是否相同。 That worked for me那对我有用

I'm using Xcode 9.2 I had my ViewControlled refactored into sections using the "extension" keyword.我正在使用 Xcode 9.2 我使用“扩展”关键字将我的 ViewControlled 重构为部分。 It appears you can't control drag into an extension of a ViewController.看来您无法控制拖动到 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.我必须重命名 ViewController.swift 文件的类(从 ViewController 到 ViewController2)并更改主故事板视图控制器的自定义类(从 ViewController 到 ViewController2),以使 Ctrl-Drag-and-drop 再次起作用。

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.在 ViewController.swift 和 Main Storyboard 中从 ViewController 2 重命名回 ViewController 并没有阻止 Ctrl-Drag-and-drop,它在之后仍然有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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