Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 合并框架更新UI无法正常工作_Ios_Swift_Combine - Fatal编程技术网

Ios 合并框架更新UI无法正常工作

Ios 合并框架更新UI无法正常工作,ios,swift,combine,Ios,Swift,Combine,我想尝试组合框架,非常简单的用法,按下UIButton并更新UILabel 我的想法是: 添加发布者 @已发布的var cacheText:String 订阅 $cacheText.assignto:\.text,在:cacheLabel上 按下按钮时指定一个值。 cacheText=testString 然后应该更新标签的文本 问题是当按下按钮时,@Published值会更新,但UILabel值不会更改。 e、 g按下按钮时,cacheLabel1最初分配为123,而不是789 以下是完整的代

我想尝试组合框架,非常简单的用法,按下UIButton并更新UILabel

我的想法是:

添加发布者 @已发布的var cacheText:String

订阅 $cacheText.assignto:\.text,在:cacheLabel上

按下按钮时指定一个值。 cacheText=testString

然后应该更新标签的文本

问题是当按下按钮时,@Published值会更新,但UILabel值不会更改。 e、 g按下按钮时,cacheLabel1最初分配为123,而不是789

以下是完整的代码:

ViewModel.swift

ViewController.swift


不知道我是否错过了什么,谢谢你的帮助。

在你有机会点击按钮之前,管道就快断了。你必须保存它,就像这样:

var storage = Set<AnyCancellable>()
override func viewDidLoad() {
    super.viewDidLoad()
    viewModel.$cacheText.assign(to: \.text, on: cacheLabel).store(in: &storage)
}

你还没来得及按下按钮,管道就快断了。你必须保存它,就像这样:

var storage = Set<AnyCancellable>()
override func viewDidLoad() {
    super.viewDidLoad()
    viewModel.$cacheText.assign(to: \.text, on: cacheLabel).store(in: &storage)
}
var storage = Set<AnyCancellable>()
override func viewDidLoad() {
    super.viewDidLoad()
    viewModel.$cacheText.assign(to: \.text, on: cacheLabel).store(in: &storage)
}