ios - applicationWillEnterForeground 为 textLabel 找到 nil

标签 ios swift null textlabel

我试图在应用程序进入前台时更改标签上的文本,但我总是得到

fatal error :在展开可选值时意外发现 nil

ViewController.swift

  @IBOutlet var textLabel: UILabel!

    func showLabel() {
        textLabel.text = "Welcome back"
    }

AppDelegate.swift

func applicationWillEnterForeground(application: UIApplication) {
       ViewController().showLabel()
    }

知道如何解决吗?

最佳答案

谢谢解答!我用

管理它
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showLabel", name: UIApplicationDidBecomeActiveNotification, object: nil)

在viewDidLoad中

然后在 AppDelegate 中

func showLabel() {}

基于: Nil when unwrapping an Optional Value in didBecomeActive()

关于ios - applicationWillEnterForeground 为 textLabel 找到 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28032505/

相关文章:

javascript - 使移动网络应用程序能够在用户移动默认浏览器中打开链接,而不是在应用程序内部打开

ios - 如何使用表格 View 制作粘性页脚和页眉?

ios - xib 文件的大小类不起作用

ios - 如何在 UITableView [Swift] 中设置特定的节标题

c - fopen() 返回 NULL 指针,但文件确实存在

c++ - 引用 null 可以吗?

ios - 如何在特定时间触发后重复本地通知

ios - 在哪里使用 'GL_EXT_color_buffer_half_float' ?

swift - 防止基于 NSDocument 的应用程序在崩溃后重新打开文档

Javascript getElementById 给出错误 'null' is null or not an object