从AnotherClass访问ViewController:UIViewController标签(这里:名为timerLabel的插座)的问题出在哪里?我想设定一个文本。
class AnotherClass{
//[...]
func updateTimeLabel(){
// To Solve!!!*******
//class name is ViewController?
let myLabel = ViewController.timerLabel
myLabel.text = "HiDuEi"
}
//[...]
// error message is:
//Instance member 'timerLabel' cannot be used on type 'ViewController'
}
如何正确访问ViewController类(称为ViewController)并在那里访问我的名为timerLabel的插座(标签)?
谢谢
最佳答案
ViewController
是类型,而不是对象的实例。您需要以某种方式获取对ViewController实例的引用,然后访问标签。例如。
let viewController = ViewController()
viewController.timerLabel.text = "HiDuEi"
请注意,以上内容创建了一个新实例,很难说出另一个viewController的创建位置以及如何从发布的代码中访问它。
关于swift - xcode4.2-从外部类快速访问Viewcontrollers标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56225059/