ios 更改 Swift UILabel 的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25043323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:36:43  来源:igfitidea点击:

Changing text of Swift UILabel

iosswiftuilabeliboutlet

提问by rocket101

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

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

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.

如何与我的应用程序的 GUI 元素进行交互?例如,我使用界面生成器制作了一个 UILabel,并通过单击控件将其连接到我的视图控制器,以便我得到 @IBOUTLET 的东西。现在,我如何在我的视图控制器中编辑这个 UILabel 的文本?换句话说,我可以使用哪些代码以编程方式控制故事板上某些内容的文本?我在网上找到的所有方法似乎只适用于代码中生成的按钮,而不是故事板上生成的按钮。

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)

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

回答by Alex Zielenski

If you've successfully linked the control with an IBOutleton your UIViewControllerclass, then the property is added to it and you would simply replace simpleLabelwith whatever you named your IBOutletconnection to be like so:

如果您已成功将控件与类IBOutlet上的an 链接UIViewController,则该属性将添加到其中,您只需将simpleLabel替换为您命名IBOutlet连接的任何内容,如下所示:

class MyViewController: UIViewController {
    @IBOutlet weak var myLabel: UILabel!
    func someFunction() {
        self.myLabel.text = "text" 
    }
}

回答by duci9y

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

您创建的插座一定是您命名的。插座属于您的视图控制器。self.simpleLabel手段“获取的价值命名属性‘simpleLabel’。

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

由于不同的网点有不同的名称,因此self.simpleLabel在您的网点命名为“simpleLabel”之前,此处无法使用。尝试将 'simpleLabel' 替换为您在创建插座时为其指定的名称。

The correct way now would be:

现在正确的方法是:

self.yourLabelName.text = "message"

回答by Matt

If you have something like this for an IBOutlet:

如果您有这样的 IBOutlet:

@IBOutlet var someLabel: UILabel!

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

然后你可以像在你的例子中一样设置 text 属性:

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:

如果您对此有疑问,可能是您没有在正确的位置分配 text 属性。如果它位于未被调用的函数中,则该行不会执行,并且 text 属性不会更改。尝试覆盖 viewDidLoad 函数,并将该行放在那里,如下所示:

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

然后,一旦视图加载,您将设置 text 属性。如果您不确定某行代码是否正在执行,您总是可以在那里放置一个断点,或者添加一些输出。就像是

println("Checkpoint")

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

在您不确定的代码块中可以真正帮助您了解它何时以及是否运行。

Hope this helps.

希望这可以帮助。

回答by itamarb

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

您可能会尝试更改不在主线程中的 UI 组件,在这种情况下,请执行以下操作:

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