更新:从 beta4 开始,问题仍然存在。
我创建了一个非常简单的示例,说明由 UIViewControllerRepresentable 表示的 UIViewController 如何永远不会被释放。
import SwiftUI
struct ContentView : View {
@State private var showRepView = true
var body: some View {
VStack {
Text("Square").font(.largeTitle).tapAction {
self.showRepView.toggle()
}
if showRepView {
SomeRepView().frame(width: 100, height: 100)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
表示实现如下:
import SwiftUI
struct SomeRepView: View {
var body: some View {
RepViewController()
}
}
struct RepViewController: UIViewControllerRepresentable
{
func makeUIViewController(context: Context) -> SomeCustomeUIViewController {
let vc = SomeCustomeUIViewController()
print("INIT \(vc)")
return vc
}
func updateUIViewController(_ uiViewController: SomeCustomeUIViewController, context: Context) {
}
static func dismantleUIViewController(_ uiViewController: SomeCustomeUIViewController, coordinator: Self.Coordinator) {
print("DISMANTLE")
}
}
class SomeCustomeUIViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("viewWillDissapear \(self)")
}
deinit {
print("DEINIT \(self)")
}
}
Run Code Online (Sandbox Code Playgroud)
通过点击“方形”按钮,SomeRepView
可以交替添加和删除。然而,相关的 UIViewController 从未被释放。这可以通过记录的消息看到,我也通过 Instruments 进行了确认。
请注意,SomeRepView 已正确发布。只有相应的视图控制器仍然被分配。
另请注意, 被UIViewController.viewWillDissappear
称为 并且也是UIViewControllerRepresentable.dismantleUIViewController
这是重复按下方形按钮的典型输出。
INIT <SomeCustomeUIViewController: 0x100b1af70>
DISMANTLE
viewWillDissapear <SomeCustomeUIViewController: 0x100b1af70>
INIT <SomeCustomeUIViewController: 0x100a0a8c0>
DISMANTLE
viewWillDissapear <SomeCustomeUIViewController: 0x100a0a8c0>
INIT <SomeCustomeUIViewController: 0x100b23690>
DISMANTLE
viewWillDissapear <SomeCustomeUIViewController: 0x100b23690>
Run Code Online (Sandbox Code Playgroud)
如图所示,DEINIT
从未打印。
我的问题是……这是一个错误吗?或者我做错了什么?
运行 iOS13 beta 4。
我尝试触发模拟内存警告。没有效果。控制器仍然存在。仪器是我的见证人;-)
归档时间: |
|
查看次数: |
3389 次 |
最近记录: |