为什么我的 UILabel 变量总是为零? [英] Why is my UILabel variable always nil?

查看:24
本文介绍了为什么我的 UILabel 变量总是为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在 Swift 中将变量与 UILabel 连接起来.我看过很多教程,但我不断收到错误消息.主要原因似乎是 @IBOutlet weak var str = UILabel! 始终为零.当我尝试为其分配一个值时,控制台会打印一个 fatal error:unwrapping an Optional value 时意外发现 nil,并且在编码界面中,该行 self.stringLabel.text = self.string 高亮显示错误 Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0.这是我的代码:

I am simply trying to connect a variable with a UILabel in Swift. I have looked at many tutorials but I keep getting errors. The main cause seems to be that @IBOutlet weak var str = UILabel! is always nil. When I try to assign a value to it, the console prints a fatal error: unexpectedly found nil while unwrapping an Optional value and inside the coding interface, the line self.stringLabel.text = self.string is highlighted with the error Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0. Here is my code:

@IBOutlet weak var stringLabel: UILabel!
var string: String!

override func viewDidLoad() {
    self.stringLabel.text = self.string
    super.viewDidLoad()
}

@IBAction func buttonClick(sender: UIButton) {
    self.stringLabel.text = "Button clicked!"
}

我已经尝试在变量周围对 .self.text 进行不同的定位,但没有成功.我在正确的轨道上吗?而且,如果是这样,我做错了什么?如果我完全错了,如何将标签的文本设置为变量的文本?预先非常感谢您.

I have tried different positioning of the .self and .text around the variables and have had no success. Am I on the right track? And, if so, what amI doing wrong? If I'm completely wrong, how does one set a label's text to that of a variable? Thank you very much in advance.

我已将故事板中的标签连接到 @IBOutlet

I have connected the label in the storyboard to the @IBOutlet

e

推荐答案

如果您在界面构建器中连接了 UILabel(确保是这种情况),Cocoa 将处理您的 UILabel,但您的字符串对象未初始化,您必须分配它与 var string: String = "" 或者如果你想使用一个可选的 var string: String?= ""

Cocoa will take care of your UILabel if you have it connected within interface builder (make sure that's the case) but your string object is uninitialized, you have to allocate it with var string: String = "" or if you want to use an optional var string: String? = ""

您可以在全局范围内执行此操作,但我建议您的所有视图控制器都有一个 init() 方法,用于分配此类内容.

You can do this in the global scope but I would recommend that all of your view controllers have an init() method where these sorts of things are assigned.

我尝试重现该问题,但得到了预期的结果.这是我的视图控制器代码

I tried reproducing the problem, but I got an expected result. Here is my code for the view controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!
    var string: String!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        string = "Hello World"
    }

    override func viewDidLoad() {
        self.testLabel.text = self.string
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

这篇关于为什么我的 UILabel 变量总是为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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