29

I am attempting to learn Apple's Swift. I was recently trying to build a GUI app, but I have a question:

How do I interact with GUI elements of my app? For instance, I used interface builder to make a UILabel, and I connected it to my viewcontroller by control-clicking, so that I get the @IBOUTLET thing. Now, how do I, while in my view controller, edit the text of this UILabel? To state it another way, what code can I use to programatically control the text of something on my storyboard? All methods I have found online only appear to work with a button generated in code, not a button generated on a storyboard.

I've seen code like

self.simpleLabel.text = "message"

If this is right, how do I link it with the label in question? In other words, how do I adapt this code to be connected with the IBOutlet (If that's what I do)

4 Answers 4

37

If you've successfully linked the control with an IBOutlet on your UIViewController class, then the property is added to it and you would simply replace simpleLabel with whatever you named your IBOutlet connection to be like so:

class MyViewController: UIViewController {
    @IBOutlet weak var myLabel: UILabel!
    func someFunction() {
        self.myLabel.text = "text" 
    }
}
4
  • 1
    Thanks a lot! Since you seem to know what you are doing, I hope you don't mind one more question: Is there any swift tutorials you would reccomend? Apple doesn't seem to have any literature on GUI programming that is an introduction level (They assume you know what you are doing). If not, no worries.
    – rocket101
    Commented Jul 30, 2014 at 17:46
  • 2
    @rocket101 Check out the WWDC 2014 videos on Swift. There are a whole bunch of them. Or check out the free "The Swift Programming Language" book in the iBooks store.
    – Rob
    Commented Jul 30, 2014 at 17:49
  • as @Rob says, the Swift Programming Language book language guide is very useful: developer.apple.com/library/prerelease/ios/documentation/Swift/… Commented Jul 30, 2014 at 17:57
  • 1
    @rocket101 I found that the course "iOS & Swift - The Complete iOS App Development Bootcamp" on Udemy is extremely useful.
    – Ramy Sameh
    Commented Jul 17, 2021 at 0:44
25

The outlet you created must've been named by you. The outlet belongs to your view controller. self.simpleLabel means 'fetch the value of my property named 'simpleLabel'.

Since different outlets have different names, using self.simpleLabel here won't work until your outlet is named 'simpleLabel'. Try replacing 'simpleLabel' with the name you gave to the outlet when you created it.

The correct way now would be:

self.yourLabelName.text = "message"
1
  • 2
    Discussion moved here. This is an answer, so no moderator action is required. Use your votes accordingly. Commented Jul 30, 2014 at 21:02
6

If you have something like this for an IBOutlet:

@IBOutlet var someLabel: UILabel!

then you could set the text property just like in your example:

someLabel.text = "Whatever text"

If you're having problems with this, perhaps you're not assigning the text property in the right place. If it's in a function that doesn't get called, that line won't execute, and the text property won't change. Try overriding the viewDidLoad function, and put the line in there, like this:

override func viewDidLoad() {
    super.viewDidLoad()
    someLabel.text = "Whatever text"
}

Then, as soon as the view loads, you'll set the text property. If you're not sure if a line of code is executing or not, you can always put a breakpoint there, or add some output. Something like

println("Checkpoint")

inside a block of code you're unsure about could really help you see when and if it runs.

Hope this helps.

6

You may trying to change a UI component not in the main thread, in that case, do this:

DispatchQueue.main.async {
        someLabel.text = "Whatever text"
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.