You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS15/SwiftUI3.0中@EnvironmentObject导致NavigationLink意外返回根视图问题求助

iOS 15/SwiftUI 3.0: Unexpected Navigation Pop to Root When App Enters Foreground (Linked to @EnvironmentObject)

Hey everyone, I've hit a super weird bug in my iOS 15/SwiftUI 3.0 app that I can't track down using existing resources. I'm hoping someone here has encountered something similar or can offer a fresh perspective.

My App's Navigation Setup

I have a 3-page flow using NavigationLink with the tag/selection pattern for programmatic navigation. Here's a snippet of how the links are structured:

NavigationLink(
    destination: CityListView(city: city, selection: $citySelection, orgId: self.orgId),
    tag: city.id,
    selection: $citySelection
) { 
    CityRow(city: city) 
}

The $citySelection binding is passed through each subsequent view, intended to let me programmatically pop back to the root when needed.

The Unexpected Behavior

Starting in iOS 15, whenever the app is brought back from the background, all views automatically pop back to the root view. I've tried removing all the selection bindings entirely, but the issue still persists. After debugging, it seems tied to one of my @EnvironmentObject instances.

The Suspected Environment Object: NotificationHandler

All three pages use @EnvironmentObject to access a NotificationHandler instance, which handles status prompts (like network activity) across the app. Here's the code for that class:

class NotificationHandler: ObservableObject {
    // Boot Notification Center
    let nc = NotificationCenter.default
    @Published var networkActive: Bool = false
    
    init() {
        print("NOTIFICATION HANDLER BOOTED")
        nc.addObserver(self, selector: #selector(networkStart), name: Notification.Name("networkingStart"), object: nil)
        nc.addObserver(self, selector: #selector(networkStop), name: Notification.Name("networkingStop"), object: nil)
    }
    
    @objc private func networkStart() {
        self.networkActive = true
    }
    
    @objc private func networkStop() {
        self.networkActive = false
    }
}

Each page checks the networkActive property to show a progress bar:

if self.notificationHandler.networkActive {
    ProgressBar()
    Spacer()
}

Key Observations

  • The bug only triggers if the first view (with the initial NavigationLink) uses this @EnvironmentObject. If I remove the @EnvironmentObject declaration from that first view, the background-to-foreground pop stops happening—but then I can't show the progress bar there.
  • Using the NotificationHandler in the other two views doesn't cause the issue.
  • This works perfectly fine in iOS 14.5; the bug is exclusive to iOS 15+.

I'm really stuck here—any ideas why this might be happening, or how to fix it without losing the progress bar functionality? Thanks so much for your help!

内容的提问来源于stack exchange,提问作者swift--help

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:22:43