【问题标题】:Button with NavigationLink and function call SwiftUI带有 NavigationLink 的按钮和函数调用 SwiftUI
【发布时间】:2021-06-24 15:15:04
【问题描述】:

我对带有条件的 NavigationLinks 有疑问。它没有反应我的预期。当用户单击按钮时,必须调用函数“test”并给出返回值。如果返回值为 true,则必须直接打开“SheetView”,而无需单击 NavigationLink 文本。请有人可以帮我解决这个问题。提前致谢

我制作了一个小(示例)程序来显示问题。

import SwiftUI

struct LoginView: View {
   @State var check = false
   @State var answer = false
   
    var body: some View {
       NavigationView {
         VStack{
            Text("it doesn't work")
            Button(action: {
                answer = test(value: 2)
                if answer {
                    //if the return value is true then directly navigate to the sheetview
                    NavigationLink(
                        destination: SheetView(),
                        label: {
                            Text("Navigate")
                        })
                }
            }, label: {
                Text("Calculate")
            })
            
        
         }
       }
    }
    
    func test(value: Int) -> Bool {
     
            if value == 1 {
                check = false
            } else {
                print("Succes")
                check = true
            }
        
        return check
    }
       
}


struct SheetView: View {

    var body: some View {
        NavigationView {
            VStack{
                Text("Test")
                    .font(.title)
            }
        }
    }
}

【问题讨论】:

    标签: swift swiftui swiftui-navigationlink swiftui-button


    【解决方案1】:

    如果您尝试显示工作表(因为您调用了导航目标 SheetView),Yodagama 的答案有效,但如果您尝试导航到 SheetView 而不是显示工作表,则以下代码将执行此操作。

    struct LoginView: View {
       @State var check = false
       @State var answer = false
       
        var body: some View {
           NavigationView {
             VStack{
                Text("it doesn't work")
                NavigationLink(destination: SheetView(), isActive: $answer, label: {
                    Button(action: {
                        answer = test(value: 2)
                    }, label: {
                        Text("Calculate")
                    })
                })
                
                
            
             }
           }
        }
        
        func test(value: Int) -> Bool {
         
                if value == 1 {
                    check = false
                } else {
                    print("Succes")
                    check = true
                }
            
            return check
        }
           
    }
    
    
    struct SheetView: View {
    
        var body: some View {
            NavigationView {
                VStack{
                    Text("Test")
                        .font(.title)
                }
            }
        }
    }
    

    【讨论】:

    • 这很好,感谢您的回答。这正是我想要的!
    【解决方案2】:
    struct LoginView: View {
       @State var check = false
       @State var answer = false
       
        var body: some View {
           NavigationView {
             VStack{
                Text("it doesn't work")
                Button(action: {
                    answer = test(value: 2)
                    //<= here
                }, label: {
                    Text("Calculate")
                })
                
            
             }
             .sheet(isPresented: $answer, content: {  //<= here
                SheetView()
             })
           }
        }
        
        ...
    

    【讨论】:

    • 感谢您的帮助,实际上我不是在寻找工作表(也许我为该结构选择了错误的名称),而只是为了获得可以导航的普通视图。知道我该怎么做吗?
    • 是的,我得到了答案。感谢你的帮助!真的很感激。
    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2020-12-29
    • 2020-02-22
    • 2020-01-08
    相关资源
    最近更新 更多