打开第二个视图,如NavigationLink onRecive()

问题描述 投票:0回答:1

您好,我试图通过接收通知来以相同的NavigationLink模式打开新视图。

var body: some View {
    return VStack {
        WebView(webPageURL:webPageURL).onReceive(NotificationCenter.default.publisher(for: .viewNotificationMessage)) { notification in

            let message = notification.object as? String ?? ""
            if(message == "Show"){
                //Open her SecoundView like NavigationLink
            }
        }
        NavigationLink(destination: SecoundView()) {
            Text("Do Something")
        }
    }
}
ios view swiftui swiftui-navigationlink
1个回答
0
投票

这里是:

@State private var isActive = false
var body: some View {
    return VStack {
        WebView(webPageURL:webPageURL).onReceive(NotificationCenter.default.publisher(for: .viewNotificationMessage)) { notification in

            let message = notification.object as? String ?? ""
            if(message == "Show"){
                self.isActive = true // activate the link below
            }
        }.background(
           NavigationLink(destination: SecoundView(), isActive: $isActive) {
            EmptyView()
        })
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.