更改Swift UILabel的文本 [英] Changing text of Swift UILabel

查看:97
本文介绍了更改Swift UILabel的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Apple的Swift。我最近正在尝试构建一个GUI应用程序,但我有一个问题:

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

如何与我的应用程序的GUI元素进行交互?例如,我使用界面构建器来创建UILabel,并通过按住Control键点击它将它连接到我的viewcontroller,以便我得到@IBOUTLET的东西。现在,在我的视图控制器中,如何编辑此UILabel的文本?换句话说,我可以使用什么代码以编程方式控制故事板上某些内容的文本?我在网上找到的所有方法似乎都只使用在代码中生成的按钮,而不是在故事板上生成的按钮。

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.

我见过像

self.simpleLabel.text = "message"

如果这是正确的,我如何将其与相关标签链接?换句话说,我如何调整此代码以与IBOutlet连接(如果这就是我所做的)

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)

推荐答案

如果你已成功将控件与 UIViewController 类中的 IBOutlet 相关联,然后将属性添加到该属性中只需将 simpleLabel 替换为您为 IBOutlet 连接命名的内容,就像这样:

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" 
    }
}

这篇关于更改Swift UILabel的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆