使用NavigationView进行交叉导航

时间:2020-05-09 23:43:31

标签: swiftui

我具有以下结构

enum Page {
    case chapter1
    case chapter2
}

struct ContentView: View {
    @State var page: Page? = nil
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: Chapter1(page: self.$page),
                               tag: .chapter1,
                               selection: self.$page) {
                    Text("Chapter 1")
                }
                NavigationLink(destination: Chapter2(page: self.$page),
                               tag: .chapter2,
                               selection: self.$page) {
                    Text("Chapter 2")
                }
            }
        }
    }
}

struct Chapter1: View {
    @Binding var page: Page?
    var body: some View {
        VStack {
            Button("Back to Overview") {
                self.page = nil
            } 
            Button("Chapter 2") {
                self.page = .chapter2
            }
        }
    }
}

struct Chapter2: View {
    @Binding var page: Page?
    var body: some View {
        VStack {
            Button("Back to Overview") {
                self.page = nil
            }
        }
    }
}

我从Chapter1直接转到Chapter2的那一刻,它中断了Chaper2中的反向链接。

首先,这是什么原因?

第二,是否可以使用另一种动画从Chapter1过渡到Chapter2,而不是重复来回翻转?

1 个答案:

答案 0 :(得分:2)

override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { Timber.d("onCreateView: called") // Define LoginViewModel loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java) // Define FragmentLoginBinding and inflate the layout binding = FragmentLoginBinding.inflate(inflater, container, false) // TODO: onCreateView: code goes here // Let the data binder know about the LoginViewModel binding.loginViewModel = loginViewModel // Return the inflated layout return binding.root } 管理器链接基于每个级别,因此修改其他堆栈级别可能会导致其意外行为。

这是上述用例的解决方案。使用Xcode 11.4 / iOS 13.4进行了测试

NavigationView