我有一个SwiftUI工作表视图,其中包含一个处理 tcp 通信的ObservedObject,当此工作表被关闭时,我需要它发送最后一条 tcp 消息,然后关闭套接字。onDisappear事件似乎从未被触发(编辑:找出罪魁祸首,因为我使用UIHostingController呈现工作表,仍然需要一个解决方案(我尝试将其放入表单、导航视图,尝试为它创建一个新堆栈,但没有任何效果。所以我尝试使用我的ObservedObjectdeinit,但如果我在关闭视图后尝试快速重新打开视图,这会给我一个错误的访问错误。
deinit {
let msg = getUpdatedTimersString()
self.connection.sendMsg(msg, success: connection.close)
}
从我的连接类中使用Network Framework
func sendMsg(_ message: String, success: @escaping () -> Void = { }, error: @escaping () -> Void = { }) {
let msg = message + "rn"
let data: Data? = msg.data(using: .utf8)
debugPrint("Sending: (msg)")
connection.send(content: data, completion: .contentProcessed { (sendError) in
if let sendError = sendError {
self.debug("(sendError)")
error()
} else {
success()
}
})
}
func close() {
connection.cancel()
}
编辑:在下面添加视图代码
struct ScheduleView: View {
@ObservedObject var scheduleManager = ScheduleManager() // This handles the tcp communication, the deinit you see above is from this
var body: some View {
NavigationView {
Form {
ForEach(scheduleManager.timers) { timer in
ScheduleForm(scheduleManager: self.scheduleManager, timer: timer).onDisappear { debugPrint("schedule form row disappeared") } // This is just a view that adds a section header and a DatePicker to the form for each timer
}
}.onDisappear { debugPrint("form disappeared") }
.navigationBarTitle(Text("Schedule"), displayMode: .inline)
}.onDisappear() { debugPrint("nav disappeared") }
}
}
这些onDisappear都不适合我,ScheduleForm行中的行是唯一为我触发的行,但它会在创建工作表时触发,每次我将一行滚动到视线之外时都会触发,但当我关闭工作表时不会。
解决方案:
final class ScheduleController: UIHostingController<ScheduleView> {
required init?(coder: NSCoder) {
super.init(coder: coder, rootView: ScheduleView())
}
init() {
super.init(rootView: ScheduleView())
}
override func viewWillDisappear(_ animated: Bool) {
rootView.scheduleManager.updateTimers() // this sends the last message
}
override func viewDidDisappear(_ animated: Bool) {
rootView.scheduleManager.connection.close // this closes the connection
}
}
我做的第一件事是更改您的函数签名以使用单个闭包来成功和完成错误。
func sendMsg(_ message: String, completion: ((Bool) ->())? = nil)
然后我们可以在 onDisappear 修饰符中使用它。完成后关闭连接。
struct ContentView: View {
var body: some View {
Text("Hello World").onDisappear {
sendMsg("last msg", completion: { completed in
close()
})
}
}
}
我经常做的是这样的:
struct ScheduleView: View {
@Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
@ObservedObject var scheduleManager = ScheduleManager()
var body: some View {
NavigationView {
Form {
ForEach(scheduleManager.timers) { timer in
ScheduleForm(scheduleManager: self.scheduleManager, timer: timer)
}
}
}.onDisappear(perform: doExit)
.navigationBarTitle(Text("Schedule"), displayMode: .inline)
}
func doExit() {
// do your closing etc ... here
// then to go back to the previous view
self.presentationMode.wrappedValue.dismiss()
}
}
我有这个非常粗糙的测试。 将显示一张工作表,当您滑动时,会触发 onDisappear 并执行 doExit 功能。对UIHostingController一无所知。
struct ContentView: View {
@State var testArray = ["one", "two", "three"]
@State var showMe = false
var body: some View {
Button(action: { self.showMe = true}) {
Text("Press button")
}.sheet(isPresented: $showMe, onDismiss: {self.showMe = false}) { TestView(scheduleManager: self.testArray) }
}
}
struct TestView: View {
@Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
@State var scheduleManager: [String]
var body: some View {
NavigationView {
Form {
ForEach(scheduleManager, id: .self) { timer in
Text(timer)
}
}
}.onDisappear(perform: doExit)
}
func doExit() {
print("-----> TestView doExit")
// then to go back to the previous view
self.presentationMode.wrappedValue.dismiss()
}
}