简体   繁体   中英

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.

I have IBOutlets in my Swift code:

@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?

Just as a note, Ctrl+Click and Right-click offer the same functionality.

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

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). 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).


Method 2

You can right-click (or ctrl+click) on the UI element in question to get the black pop-up you mentioned. 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.

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