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

Open second View like NavigationLink onRecive()

您好,我尝试通过接收通知以与 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")
        }
    }
}

这里是:

@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()
        })
    }
}