简体   繁体   English

如何在XCode UI中将情节提要线连接到Swift代码?

[英]How do I wire up the storyboard to Swift code in XCode UI?

This seems like a basic question, but I haven't been able to pin it down. 这似乎是一个基本问题,但我无法确定。

I have an Apple Watch Glance that is meant to have, going from the top down, a thin label and then a thick label in the upper area, and then an image in the bottom area. 我有一个Apple Watch Glance,它的意思是从上到下依次是:薄标签,上标签,下标签和下标签。

I have IBOutlets in my Swift code: 我的Swift代码中有IBOutlets:

@IBOutlet var clockFaceImage: WKInterfaceImage!
@IBOutlet var hourLabel: WKInterfaceLabel!
@IBOutlet var modernLabel: WKInterfaceLabel!

When I right-click on the labels on the storyboard, it has "Referencing Outlets" with an option of "New referencing outlet". 当我右键单击情节提要上的标签时,它具有“参考插座”和“新参考插座”选项。 Nothing interesting happens when I click or right-click on "New referencing outlet". 当我单击或右键单击“新引用出口”时,没有任何有趣的事情发生。 The circle to the right converts to a plus sign when I hover, but nothing interesting happens when I click on that. 当我将鼠标悬停时,右侧的圆圈将转换为加号,但是当我单击该圆圈时,不会发生任何有趣的事情。

What should I be doing to set things up so that GlanceController methods can assign values to the image and labels? 我应该怎么做才能进行设置,以便GlanceController方法可以为图像和标签分配值?

Just as a note, Ctrl+Click and Right-click offer the same functionality. 只是要注意,Ctrl +单击和右键单击提供相同的功能。

There are two ways to hook up outlets from your interface builder file. 有两种方法可以从接口构建器文件连接插座。 Both of them require having your assistant editor open, with the Interface builder in one pane (usually the left) and the relevant source code open in the other pane. 两者都需要打开您的助手编辑器,在一个窗格中(通常是左侧)打开“界面”构建器,并在另一窗格中打开相关的源代码。


Method 1 方法1

You can ctrl+click directly on the UI element in question and without unclicking, drag it straight into the source control file where you want to add a property (or over an existing property). 您可以直接按住Ctrl键并单击所涉及的UI元素,而无需取消单击,将其直接拖动到要在其中添加属性的源控制文件中(或在现有属性上)。 If the property already exists, you can drag to that property and hook it up. 如果该属性已经存在,则可以将其拖动到该属性并将其连接起来。 Otherwise, you can drag to empty space and when you release the click, you will get a pop-up dialog asking you some information about what sort of outlet/action you want to create (IBOutlets, IBOutletCollections, and IBActions are all created in the same way). 否则,您可以将其拖到空白处,然后释放单击,您将弹出一个对话框,询问有关您要创建哪种出口/操作的信息(IBOutlet,IBOutletCollections和IBActions都在同样的方式)。


Method 2 方法二

You can right-click (or ctrl+click) on the UI element in question to get the black pop-up you mentioned. 您可以在有问题的UI元素上单击鼠标右键(或ctrl + click),以得到您提到的黑色弹出窗口。 From here, find the circle for the thing you want to hook up (in this case outlet), click and drag from there to the source code where you want to hook it up, just as previously described. 从这里找到您要连接的物体的圆(在本例中为出口),单击并从那里拖动到您想要连接的源代码,如前所述。


Generally speaking, I personally usually use the first approach, however the second approach certainly has value when you need to hook up different actions of the same UI element, or if you just need to check whether or not it even has been hooked up yet. 一般而言,我个人通常使用第一种方法,但是当您需要连接同一UI元素的不同操作时,或者仅需要检查它是否已被连接时,第二种方法肯定有价值。

暂无
暂无

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

相关问题 如何在XCode故事板中删除segue? - How do I delete a segue in XCode storyboard? Xcode故事板显示UI元素周围的蓝框。 我怎么关掉这个? - Xcode Storyboard Shows Blue Frames Around UI Elements. How do I turn this off? 如何使用 Swift 将 storyboard 中的“已发送事件”连接到 Xcode 13.3 中的事件处理程序 function? - How do I connect "Sent events" in storyboard to an event handler function in Xcode 13.3 using Swift? 如何在自定义UIView中连接UICollectionView-Swift - How to wire up UICollectionView in a custom UIView - Swift 如何从XCode故事板“转到”参考插座的代码? - How do I “go to” a referencing outlet's code from the XCode storyboard? XCode / Swift 3:如何在不破坏现有约束的情况下上下移动情节提要元素? - XCode/Swift 3: How to shift storyboard elements down/up without messing up existing constraints? 如何在swift文件中获取类的引用,并在另一个swift文件中进行编码? -xcode /迅速 - How do i get a reference of a class in a swift file and code that in another swift file? - xcode / swift Xcode 4.4改变了本地化 - 我怎么不本地化主故事板? - Xcode 4.4 changes to localization - how do I NOT localize main storyboard? 如何在Xcode故事板中更改预览设备? - How do I change the preview device in Xcode storyboard? xcode storyboard容器视图 - 如何访问viewcontroller - xcode storyboard Container View - How do I access the viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM