[英]Removing SwiftUI NavigationView from view hierarchy result in EXC_BAD_ACCESS
I am struggling with a bug and I just can't seem to solve it, or where to look further.我正在与一个错误作斗争,我似乎无法解决它,或者在哪里进一步寻找。
The problem occurs when I try to remove a view (which holds a NavigationView) from the view hierarchy.当我尝试从视图层次结构中删除视图(包含 NavigationView)时,就会出现问题。 It crashes with:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)
它崩溃了:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)
After experimenting with the sanitizer I got this output in the debugger: *** -[_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext_ removeChildViewController:]: message sent to deallocated instance 0x10904c880
在试用了消毒剂后,我在调试器中得到了这个 output:
*** -[_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext_ removeChildViewController:]: message sent to deallocated instance 0x10904c880
Which pointed me to figure out that it was the NavigationView that cause it somehow.这让我发现它是以某种方式导致它的 NavigationView 。 But I still can't figure out how to get from here.
但我仍然不知道如何从这里出发。
This problem ONLY occurs on a real device, it works just fine in a simulator and you may have to hit the login, and then log out and log back in a few times before the crash happens.此问题仅发生在真实设备上,它在模拟器中运行良好,您可能必须登录,然后在崩溃发生之前注销并重新登录几次。
I made a sample app with the example: https://github.com/Surferdude667/NavigationRemoveTest我用示例制作了一个示例应用程序: https://github.com/Surferdude667/NavigationRemoveTest
The code is as follows:代码如下:
NavigationRemoveTestApp NavigationRemoveTestApp
@main
struct NavigationRemoveTestApp: App {
var body: some Scene {
WindowGroup {
RootView()
}
}
}
RootView根视图
struct RootView: View {
@StateObject private var viewModel = RootViewModel()
var body: some View {
if !viewModel.loggedIn {
WelcomeView()
} else {
ContentView()
}
}
}
RootViewModel根视图模型
class RootViewModel: ObservableObject {
@Published var loggedIn = false
init() {
LogInController.shared.loggedIn
.receive(on: DispatchQueue.main)
.assign(to: &$loggedIn)
}
}
WelcomeView欢迎查看
struct WelcomeView: View {
var body: some View {
NavigationView {
VStack {
Text("Welcome")
NavigationLink("Go to login") {
LogInView()
}
}
}
}
}
LogInView登录查看
struct LogInView: View {
var body: some View {
VStack {
Text("Log in view")
Button("Log in") {
LogInController.shared.logIn()
}
}
}
}
ContentView内容视图
struct ContentView: View {
var body: some View {
VStack {
Text("Content view")
Button("Log out") {
LogInController.shared.logOut()
}
}
}
}
LogInController登录控制器
import Combine
class LogInController {
static let shared = LogInController()
var loggedIn: CurrentValueSubject<Bool, Never>
private init() {
self.loggedIn = CurrentValueSubject<Bool, Never>(false)
}
func logIn() {
self.loggedIn.send(true)
}
func logOut() {
self.loggedIn.send(false)
}
}
I found a few solutions.我找到了一些解决方案。
Either you wrap the if statement in the RootView
with a NavigationView
instead of having the NavigationView
inside the actual views, it works.要么将 if 语句包装在带有
NavigationView
的RootView
中,而不是将NavigationView
放在实际视图中,它都可以工作。 This is however not very convenient since everything is now wrapped in a NavigationView.然而,这不是很方便,因为现在所有内容都包含在 NavigationView 中。
Replacing NavigationView with the new iOS 16 NavigationStack
also solves it.用新的 iOS 16
NavigationStack
替换 NavigationView 也解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.