无法从Swift中的其他类更新Label [英] Can't update Label from other class in Swift

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

问题描述

我的代码有一些问题,因此我无法调用函数。我在ViewController中创建了一个函数 func changeLabelText(text:String){mylabel.stringValue = text} ,我可以更新Label中的Text。当我尝试从Project中的另一个类调用该函数时,遇到运行时错误( EXC_BAD_Instr ... ),并且调试器在我尝试更改的行中保留标签的文本,错误为:致命错误:在展开可选值时意外发现nil。问题出在哪里?有人可以帮帮我吗!

I have a little problem with my code so that I can't call a function. I created a function func changeLabelText(text: String) { mylabel.stringValue = text } in the ViewController with that I can update the Text in the Label. When I try to call the function from another class in the Project, I get a runtime error(EXC_BAD_Instr...) and the Debugger holds on the line where I try to change the Label's text, with the error: fatal error: unexpectedly found nil while unwrapping an Optional value. Where is the problem? Can someone help me please!

推荐答案

关于在包装可选值时意外找到nil错误,问题是您有一个可选的 nil (根据您提供的代码段,它必须是一个隐式解包的可选)。 mylabel 最有可能是 nil 。要么打印值,要么添加断点并在调试器中检查属性以确认。

Regarding the "unexpectedly found nil while unwrapping an Optional value" error, the problem is that you have an optional (judging from the code snippet you've provided, it must be an implicitly unwrapped optional) that is nil. Most likely, mylabel is nil. Either print the value or add a breakpoint and examine the property in the debugger to confirm.

如果它是 nil ,则必须弄清楚原因。在我们的讨论中,事实证明您正在尝试在视图控制器中调用 Log 事件:

If it is nil, you then have to figure out why. In our discussion, it turns out that you're trying to call the Log event in the view controller:

ViewController().Log("asdf")

问题是这不会在现有视图控制器中调用 Log ,而是 ViewController()表达式最终实例化一个新的,完全不相关的视图控制器,其出口未连接任何东西。因此,尝试更新隐式解开的出口将导致您与我们共享的错误。

The problem is that this doesn't call Log in the existing view controller, but rather the ViewController() expression ends up instantiating a new, completely unrelated view controller with its outlets not hooked up to anything. Thus the attempt to update the implicitly unwrapped outlet will cause the error you shared with us.

如果您希望此单独的类(数据库管理器对象)通知视图控制器为了允许视图控制器更新UI,通常使用三种方法:

If you want this separate class (a database manager object) to inform the view controller of event in order to allow the view controller to update the UI, there are three common approaches:


  1. 完成/进度处理程序是闭包/块。

  1. Completion/progress handlers that are closures/blocks.

这用于简单的界面,在数据库需要完成请求时,数据库需要通知视图控制器。

This is used for simple interface where the database needs to inform view controller when the request is done.

代理协议模式。

授权模式(通常符合一些公认的协议)用于富int数据库需要将各种不同类型的事件通知视图控制器的地方。

The delegation pattern (usually conforming to some well-established protocol) is used for rich interfaces where the database needs to inform the view controller of a variety of different types of events.

通知模式。

通知。视图控制器可以使用 NSNotificationCenter defaultCenter()将其自身注册为特定名称通知的观察者。 。然后,数据库对象可以发布该名称的通知(通过 userInfo 词典提供详细信息),然后视图事件将通知视图控制器。

Notifications are used when you want a loosely coupled interface between the database object and whatever is handling these notifications. The view controller might register itself as an observer of any notifications of a particular name with the defaultCenter() of the NSNotificationCenter. The database object can then post notifications of that name (supplying details via the userInfo dictionary) and the view controller will be informed of these events.

这篇关于无法从Swift中的其他类更新Label的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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