使用 SwiftUI 转到新视图 [英] Go to a new view using SwiftUI

查看:61
本文介绍了使用 SwiftUI 转到新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 SwiftUI 的带有按钮的基本视图,我正在尝试在点击按钮时呈现一个新的屏幕/视图.我该怎么做呢?我是否想为这个视图创建一个委托,告诉应用程序的 SceneDelegate 呈现一个新的视图控制器?

I've got a basic view with a button using SwiftUI and I'm trying to present a new screen/view when the button is tapped. How do I do this? Am I suppose to create a delegate for this view that will tell the app's SceneDelegate to present a new view controller?

import SwiftUI

struct ContentView : View {
    var body: some View {
        VStack {
            Text("Hello World")
            Button(action: {
                //go to another view
            }) {
                Text("Do Something")
                    .font(.largeTitle)
                    .fontWeight(.ultraLight)
            }
        }
    }
}

推荐答案

关键是使用一个NavigationView和一个NavigationLink:

The key is to use a NavigationView and a NavigationLink:

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
                NavigationLink(destination: DetailView()) {
                    Text("Do Something")
                }
            }
        }
    }
}

这篇关于使用 SwiftUI 转到新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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