简体   繁体   English

SwiftUI onAppear 在 TabView 内的 NavigationView 被调用两次

[英]SwiftUI onAppear called twice when NavigationView inside TabView

So I have a TabView where each of the tabs is embedded in a NavigationView.所以我有一个 TabView,其中每个选项卡都嵌入在 NavigationView 中。 On first appear of each tab I get the following lifecycle calls onAppear(), onDisappear(), onAppear().在每个选项卡第一次出现时,我得到以下生命周期调用 onAppear()、onDisappear()、onAppear()。 So it looks like onAppear gets called twice.所以看起来 onAppear 被调用了两次。 This only happens the first time.这只是第一次发生。 If I navigate back to the same tab, only onAppear() gets called, and only once.如果我导航回同一个选项卡,只有 onAppear() 被调用,而且只有一次。

Here's a minimal example:这是一个最小的例子:

struct Page1: View {
    init() { print("Page 1 init") }
    
    var body: some View {
        NavigationView {
            Text("Page 1")
                .onAppear(perform: { print("Page 1 appearing") })
                .onDisappear(perform: { print("Page 1 disappearing") })
        }
    }
}

struct Page2: View {
    init() { print("Page 2 init") }
    
    var body: some View {
        NavigationView {
            Text("Page 2")
                .onAppear(perform: { print("Page 2 appearing") })
                .onDisappear(perform: { print("Page 2 disappearing") })
        }
    }
}

struct ContentView: View {
    var body: some View {
        TabView {
            Page1().tabItem { Text("Page 1") }
            Page2().tabItem { Text("Page 2") }
        }
    }
}

And here's the result printed out:这是打印出来的结果:

Page 1 init
Page 2 init
Page 1 appearing
Page 1 disappearing
Page 1 appearing

Here's what happens if I click on the second tab如果我单击第二个选项卡,会发生以下情况

Page 1 init
Page 2 init
Page 1 appearing
Page 1 disappearing
Page 1 appearing
// here I clicked on second tab
Page 2 appearing
Page 2 disappearing
Page 2 appearing
Page 1 disappearing
    TabView {
        NavigationView {
            VStack {
                Color.red
                    .onAppear {
                        print("appear : red")
                    }
                    .onDisappear {
                        print("disappear : red")
                    }
            }.onAppear {
                print("appear")
            }
        }
    }

Test on iOS 15 beta Simulator在 iOS 15 beta 模拟器上进行测试

The output: output:

appear : red
appear
disappear : red
appear : red

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当视图在 TabView 内时,为什么会调用两次`.onAppear` - Why is `.onAppear` called twice when view is inside TabView 为什么 onAppear() 在 swiftUI 中的 NavigationView 内放置元素时会执行两次? (Xcode 12.0) - Why does onAppear() execute twice when placed on elements inside a NavigationView in swiftUI? (Xcode 12.0) NavigationView 内的 SwiftUI TabView - SwiftUI TabView inside a NavigationView 向后导航时如何避免应用程序在 NavigationView 内使用 TabView 崩溃 ios 13 SwiftUI - How to avoid app crash with TabView inside NavigationView when navigate backwards ios 13 SwiftUI SwiftUI 中的 UITabViewController 的 Tab 的 OnAppear 被调用了两次 - UITabViewController in SwiftUI has its Tab's OnAppear Being Called Twice 在NavigationLink之后,在NavigationView内部,TabView内部显示NavigationBar。 SwiftUI - Displaying a NavigationBar, inside a NavigationView, inside a TabView, after a NavigationLink. SwiftUI SwiftUI,将标题设置为 NavigationView 内 TabView 的子视图不起作用 - SwiftUI, setting title to child views of TabView inside of NavigationView does not work SwiftUI - 如何将工具栏添加到 NavigationView 内的 TabView 选项卡? - SwiftUI - How to add toolbars to TabView tabs inside a NavigationView? 在 SwiftUI 中更改具有 NavigationView 和 ScrollView 的 TabView 内视图的背景颜色 - Change background color of View inside TabView having NavigationView and ScrollView in SwiftUI 为什么在 SwiftUI 的 TabView 中切换选项卡时 onDisappear 后再次调用 onAppear? - Why onAppear called again after onDisappear while switching tab in TabView in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM