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

时间:2020-05-11 13:20:04

标签: ios view swiftui swiftui-navigationlink

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

1 个答案:

答案 0 :(得分:1)

这里是:

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