如何通过单击按钮转到另一个视图 [英] How to go to another view with button click

查看:44
本文介绍了如何通过单击按钮转到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个按钮,我有一个名为 LogindView.swift 的文件

单击按钮时,我无法获取打开另一个视图文件的代码.

谁能举个例子告诉我怎么做.

在我的按钮操作中,我尝试编写 LogindView() 但我只是给了我一个警告.'LogindView' 初始值设定项的结果未使用"

 按钮(动作:{//做动作登录视图()}, 标签: {//** 标签文本文本(登录").font(.headline).padding(.all).foregroundColor(Color.white)}).background(Color.blue)

解决方案

您基本上有 3 个选项可以根据需要在视图之间转换.

<小时>

首先,您可以使用NavigationView.这将提供一个返回按钮并允许用户返回.请注意,当您不按照

<小时>

第二,您可以使用.sheet 来呈现模态.这将显示一个显示在当前视图顶部的模式,但用户可以通过向下拖动来关闭它.

导入 SwiftUI结构主视图:查看{@State var isModal: Bool = falsevar主体:一些视图{按钮(登录"){self.isModal = 真}.sheet(isPresented: $isModal, 内容: {登录视图()})}}结构登录视图:查看{var主体:一些视图{文本(登录视图")}}

<小时>

第三,您可以像这样使用 if 语句将当前视图更改为您的登录视图

导入 SwiftUI结构主视图:查看{@State var showLoginView: Bool = falsevar主体:一些视图{虚拟堆栈{如果 showLoginView {登录视图()} 别的 {按钮(登录"){self.showLoginView = true}}}}}结构登录视图:查看{var主体:一些视图{文本(登录视图")}}

如果您想为此设置动画,以便过渡不会如此突然地出现,您也可以这样做:

导入 SwiftUI结构主视图:查看{@State var showLoginView: Bool = falsevar主体:一些视图{虚拟堆栈{如果 showLoginView {登录视图().animation(.spring()).transition(.slide)} 别的 {按钮(登录"){带动画{self.showLoginView = true}}.动画(.无)}}}}结构登录视图:查看{var主体:一些视图{文本(登录视图")}}

I have a button in my code and I have a file called LogindView.swift

I cannot get the code to open another view file when clicking on the button.

Can anybody give me an example on how to do it.

In my button action I have tried to write LogindView() but i just gives me a warning. "Result of 'LogindView' initializer is unused"

    Button(action: {
            // Do action
            LogindView()
        }, label: {
            //** Label text
            Text("Logind")
                .font(.headline)
                .padding(.all)
                .foregroundColor(Color.white)
        })
        .background(Color.blue)

解决方案

You essentially have 3 options to transition between views depending on your needs.


First, you can use a NavigationView. This will provide a back button and will allow the user to go back. Note that there are some bugs currently when you don't put the NavigationLink inside of a List as per https://stackoverflow.com/a/57122621/3179416

import SwiftUI

struct MasterView: View {
        var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: LoginView()) {
                    Text("Login")
                }
            }
            .navigationBarTitle(Text("Master"))
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}


Second, you can present a modal using .sheet. This will present a modal that appears on top of the current view but it can be dismissed by the user by dragging it down.

import SwiftUI

struct MasterView: View {
    @State var isModal: Bool = false

    var body: some View {
        Button("Login") {
            self.isModal = true
        }.sheet(isPresented: $isModal, content: {
            LoginView()
        })
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}


Third, you can just use an if statement to change the current view to your Login View like so

import SwiftUI

struct MasterView: View {
    @State var showLoginView: Bool = false

    var body: some View {
        VStack {
            if showLoginView {
                LoginView()
            } else {
                Button("Login") {
                    self.showLoginView = true
                }
            }
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

If you would like to animate this, so that the transition doesn't appear so abruptly, you can also do this:

import SwiftUI

struct MasterView: View {
    @State var showLoginView: Bool = false

    var body: some View {
        VStack {
            if showLoginView {
                LoginView()
                    .animation(.spring())
                    .transition(.slide)
            } else {
                Button("Login") {
                    withAnimation {
                        self.showLoginView = true
                    }
                }.animation(.none)
            }
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

这篇关于如何通过单击按钮转到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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