SwiftUI - NavigationLink 无法使用按钮 [英] SwiftUI - NavigationLink is not working with a button

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

问题描述

我正在制作一个应用程序,我在其中输入两个数字,并希望在单击按钮时在第二个屏幕中显示数字的加法结果.我可以在控制台中打印结果,但不幸的是,按钮周围的导航链接似乎不起作用.如果我将 NavigationLink 放在按钮标签周围而不是整个按钮周围,那么它会转到第二个屏幕但按钮操作停止工作.这是我的代码:

I am making an app where I take two number inputs and want to show the addition result of the numbers in the second screen, when a button is clicked. I can print the result in the console, but unfortunately it seems like navigation link is not working, around the button. If I put NavigationLink around the button label instead of around the whole button, then, it goes to the second screen but button action stops working. Here is my code:

import SwiftUI

struct ContentView: View {
    @State private var number1 : String = ""
    @State private var number2: String = ""
    @State private var isTapped:Bool = false
    @State var sum : Double = 0
    var body: some View {
        
        NavigationView {
            VStack{
                TextField("Type first number", text: $number1)
                    .keyboardType(.numberPad).padding()
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .background(Color.gray).border(Color.blue,width:5)
                
                TextField("Type second number", text: $number2)
                    .keyboardType(.numberPad).padding()
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .background(Color.gray).border(Color.blue,width:5)
                
                
                //this Navigationlink is not working
                NavigationLink(destination: Text("\(self.sum)")) {
                    Button(action: {
                        print("I am here in the action")
                        self.isTapped.toggle()
                        UIApplication.shared.endEditing()
                        if let num1 = Double(self.number1), let num2 = Double(self.number2){
                            print("I am here")
                            self.sum = num1 + num2
                            print("\(self.sum)")
                        }
                    }) {
                        //If I put the Navigationlink here, button action stop working.
                        Text("Add Two Numbers")
                            .padding()
                            .foregroundColor(.blue)
                            .background( isTapped ? Color.orange : Color.gray)
                            .font(.title)
                            .border(Color.blue, width: 5)
                            .shadow(radius: 10)
                        
                    }
                    
                }
                
            }
            
        }
        
    }
}
extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

有什么线索吗?感谢您的好奇心.

Any clue? Thanks for your curiosity.

推荐答案

NavigationLink 本身就是一个按钮,实际上,所以你引入了某种冲突.相反,您可以将链接仅与其他点击手势处理程序一起使用,例如

NavigationLink is itself a button, actually, so you introduce some kind of conflict. Instead you can use link just with additional tap gesture handler, like

NavigationLink(destination: Text("\(self.sum)")) {
    Text("Add Two Numbers")
        .padding()
        .foregroundColor(.blue)
        .background(isTapped ? Color.orange : Color.gray)
        .font(.title)
        .border(Color.blue, width: 5)
        .shadow(radius: 10)
    }
    .simultaneousGesture(TapGesture().onEnded{
        print("I am here in the action")
        self.isTapped.toggle()
        UIApplication.shared.endEditing()
        if let num1 = Double(self.number1), let num2 = Double(self.number2){
            print("I am here")
            self.sum = num1 + num2
            print("\(self.sum)")
        }
    })

这篇关于SwiftUI - NavigationLink 无法使用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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