(Swift) Specifiying By Tags

(Swift) Specifiying By Tags

In Swift, you can add items to your storyboard and refer to it in your code. For example, you can add a UIButton and then create an IBOutlet in your code to ‘connect’ to it.

Another way to do this is by giving it a tag number. Although this way might not be the best way to “grab” and assign things because I feel like it’s very thinking to assigning an ID to a tag in web development.

Tools:

  • Swift 2.2
  • Xcode Version 7.3.1

To give your item a tag,

> Click on your item
> Select "Attributes Inspector" in your "Utilities"panel (this is the wedge-looking icon)
> Scroll down to find the "View" section and find the "Tag" field
> Give it a unique number
"Giving a UIView a tag"

Once you assign something with a tag you can then look it up by that tag and then augment it.

Here is an example:

if let theLabel = self.view.viewWithTag(1) as? UILabel {
  theLabel.text = "I'm replacing text here"
}

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *