xcode 如何将 UIView 插座连接到自定义子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12975880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:52:37  来源:igfitidea点击:

How to connect UIView outlets to a custom subview

iosxcode

提问by user1702623

I'm still new to xcode / iOS and have the following problem:

我还是 xcode / iOS 的新手并且有以下问题:

in order to display some mobile debug information, I have a UIview added/connected as outlet-property to one of my Viewcontroller. This view is a custom subclass of UIview. Now I addeddd some UIlabels as sub views to this view and want to drag the outlet connections from these labels to my customUIview.h file in order to have these labels accessible as properties of my custom UIview class (no need to access them directly from the view Controller).

为了显示一些移动调试信息,我添加/连接了一个 UIview 作为插座属性到我的一个 Viewcontroller。此视图是 UIview 的自定义子类。现在我添加了一些 UIlabels 作为该视图的子视图,并希望将这些标签中的插座连接拖到我的 customUIview.h 文件中,以便将这些标签作为我的自定义 UIview 类的属性进行访问(无需直接从视图控制器)。

Problem is that the interface builder (I'm using Storyboards/ xcode4.3) does not make the trick. I can connect the outlets to the ViewControllerClass.h but not to my sub view's .h file. Can anyone point out where the problem is?

问题是界面构建器(我使用的是 Storyboards/xcode4.3)没有成功。我可以将插座连接到 ViewControllerClass.h 但不能连接到我的子视图的 .h 文件。谁能指出问题出在哪里?

回答by tib

Just solved the problem. After typing the property outlet declarations manually in the customview.h file I could ctrl-drag-connect them from there to their corresponding UIlabel objects in the interfacebuilder. Works only in this direction!

刚刚解决了问题。在 customview.h 文件中手动输入属性插座声明后,我可以按 ctrl-drag-connect 将它们从那里连接到 interfacebuilder 中对应的 UIlabel 对象。只在这个方向上起作用!

Thanks anyway

不管怎么说,还是要谢谢你

回答by kidsid49

Update your custom view class from "UIView" to your "Custom View" class in interface builder. And now you can make connections just by ctrl+drag your buttons or textfield to your appropriate custom class.

将您的自定义视图类从“UIView”更新为界面构建器中的“自定义视图”类。现在,您只需按 ctrl+拖动按钮或文本字段到适当的自定义类即可建立连接。

回答by kidsid49

Problem solved! Due to an interesting SO post from three years ago (about connecting to subviews of UIView), I discovered that one merely drags (not Ctrl_drag!) from the action or outlet circle (in the .h file) to the control and that's it. Works perfectly even when the controls are in a different view from the subclassed UIView. Works equally well with outlets as with actions though you always drag away from the circle.

问题解决了!由于三年前的一篇有趣的 SO 帖子(关于连接到 UIView 的子视图),我发现一个人只是将(不是 Ctrl_drag!)从动作或出口圆圈(在 .h 文件中)拖到控件上,仅此而已。即使控件位于与子类 UIView 不同的视图中,也能完美运行。尽管您总是远离圆圈,但与出口和动作同样适用。