打开第二个视图,如 NavigationLink onRecive() [英] Open second View like NavigationLink onRecive()

查看:41
本文介绍了打开第二个视图,如 NavigationLink onRecive()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试通过接收通知以与 NavigationLink 相同的模式打开一个新视图.

Hello i try to open a new view in the same pattern of the NavigationLink by receiving a notification.

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

这篇关于打开第二个视图,如 NavigationLink onRecive()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆