簡體   English   中英

iOS 14.5 NavigationLink 在 withAnimation 中清除環境對象

[英]iOS 14.5 NavigationLink nils EnvironmentObjects when in the middle of withAnimation

目前在我的項目中,我有一個導航堆棧,在父級 NavigationView 中,有一個 Timer 啟動一個 withAnimation 塊,該塊為導航堆棧中的按鈕共享的屬性設置動畫。

在更新到 iOS 14.5 之后,當 NavigationLink 在 withAnimation 塊(在這種情況下總是如此)的中間被點擊時,向下傳遞導航堆棧的 EnvironmentObjects 變為 nil。

拋出的錯誤:致命錯誤:找不到 BackgroundViewModel 類型的 ObservableObject。 作為該視圖的祖先,BackgroundViewModel 的 View.environmentObject(_:) 可能會丟失。

絕對認為它是 NavigationView 處理動畫的新方式和 NavigationLinks 處理 state 方式的組合。 根據 iOS 14.5 開發人員發布說明: NavigationView push and pop now correctly respects disabled animations. (70062477) NavigationView push and pop now correctly respects disabled animations. (70062477) & The destination of NavigationLink that only differs by local state now resets that state when switching between links as expected. (72117345) The destination of NavigationLink that only differs by local state now resets that state when switching between links as expected. (72117345)

下面是一個將重新創建錯誤的示例。 任何幫助將不勝感激。

import SwiftUI
import Combine

struct ContentView: View {
    @StateObject var backgroundVM = BackgroundViewModel()
    @State var presentNext: Bool = false
    
    var body: some View {
        NavigationView {
            VStack{
                Text("Hello, world!")
                    .padding()
                NavigationLink(
                    destination: View2(),
                    isActive: $presentNext) {
                    Button {
                        presentNext = true
                    } label: {
                        Text("Navigate")
                    }
                }
            }
            .frame(width: 250, height: 250, alignment: .center)
            .background(backgroundVM.backgroundColor)
        }
        .environmentObject(backgroundVM)
        .onReceive(backgroundVM.timer) { _ in
            withAnimation(.easeInOut(duration: 2.0)) {
                backgroundVM.setColor()
            }
        }
    }
}

struct View2: View {
    @EnvironmentObject var backgroundVM: BackgroundViewModel
   
    @State var presentNext: Bool = false
    
    var body: some View {
        VStack {
            Text("View 2")
            NavigationLink(
                destination: View3(),
                isActive: $presentNext) {
                Button {
                    presentNext = true
                    
                } label: {
                    Text("Navigate")
                }
            }
        }
        .frame(width: 250, height: 250, alignment: .center)
        .background(backgroundVM.backgroundColor)
    }
}

struct View3: View {
    @EnvironmentObject var backgroundVM: BackgroundViewModel
    
    var body: some View {
        VStack {
            Text("View 3")
        }
        .frame(width: 250, height: 250, alignment: .center)
        .background(backgroundVM.backgroundColor)
 
    }
}

class BackgroundViewModel: ObservableObject {
    @Published var backgroundColor: Color = .orange
    @Published var colorIndex: Int = 0 {
        willSet {
            backgroundColor = colors[newValue]
        }
    }
    
    var timer = Timer.publish(every: 1.5, on: .main, in: .common)
        .autoconnect()
    
    var colors: [Color] = [.orange, .green, .purple]
    
    func setColor() {
        if colorIndex + 1 == colors.count {
            colorIndex = 0
        } else {
            colorIndex += 1
        }
    }
}

嘗試

View2().environmentObject(backgroundVM)

 View3().environmentObject(backgroundVM)

你必須使用這個

 .environmentObject(backgroundVM)

每次實例化一個具有

 @EnvironmentObject

我有同樣的問題。 您可以同時從以下答案中使用 singleton 查看此解決方案:

SwiftUI 中的 EnvironmentObject 與 Singleton?

這也發生在 iOS 14.6(剛剛發布)上,以及 SwiftUI 上的導航錯誤。 所以要小心:

iOS 14.5 的 SwiftUI NavigationLink 不工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM