Swiftui 在另一个视图中使用来自 1 个视图的计算值

问题描述

我对 Swiftui 比较陌生,目前正在开发我的第二个应用程序,它比第一个复杂得多。 我在一个特定的需求上碰壁了,几天来我一直在努力解决这个需求,现在我使用了大量关于 State/Binding/ObservableObject 等的视频和文章,我正在兜兜转转。

我正在开发一个高尔夫计分应用程序,用户将依次通过每个洞并输入他们的分数,将完成各种计算并显示在视图中。其中之一是总得分。当他们移动到第二个洞时,将遵循相同的过程,但有一个额外的计算,需要将第 1 洞和第 2 洞的总点数相加。我尝试过的各种方法要么以 0 或我想记住更多错误消息,当然太多了,无法包含在这里!!

我希望得到任何帮助,我在下面包含了我的代码(为了这篇文章的目的而减少),包括一些注释来解释我需要做什么,我希望这些注释很清楚。如果任何人都可以将我的代码调整为有用的东西,那么我可以看看它们是如何组合在一起的。

提前致谢

开始查看

// 此屏幕仅用于开始一轮。它将带您到第 1 洞,然后第 1 洞中的导航链接将带您到第 2 洞,依此类推。

import SwiftUI

struct ContentView: View {
    
var body: some View {
        NavigationView {
                NavigationLink(destination: Hole1View()) {
                    Text("Start Round")
                        .frame(width: 300,height: 30)
                        .font(.system(size: 18,weight: .bold,design: .rounded))
                        .lineLimit(1)
                        .multilineTextAlignment(.center)
                        .padding(18)
                        .background(Color.yellow)
                        .foregroundColor(Color.blue)
                        .cornerRadius(10)
                        .border(Color.blue,width: 10)
                        .cornerRadius(1)
                    }
                }
            }
        }
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

第 1 洞视野

// 在此视图中,您将输入各种信息,并且视图中将包含各种计算。其中一个计算是计算下一个视图中计算所需的值 (Hole1totalpoints)。

import SwiftUI

struct Hole1View: View {
    @State private var hole1gross = 0
    
    
    var hole1totalpoints: Int {
        let hole1points = hole1gross * 3
        return hole1points
    }
    
    
    var body: some View {
        
        // On this view you will enter various information and there will be various calculations to include in the View. One of the calculations is to calculate a value (Hole1totalpoints) which will be required in calculations in the next view.
        
        
        
        
        
        Stepper("\(hole1gross)",value: $hole1gross,in: 0...15)
            .frame(width: 300,height: 35,alignment: .leading)
            .font(.system(size: 16,design: .rounded))
            .minimumScaleFactor(0.6)
            .multilineTextAlignment(.leading)
            .lineLimit(2)
        
        Text("Calculated Value \(hole1totalpoints)")
        
        
        
        NavigationLink(destination: Hole2View()) {
            Text("Go to hole 2")
        }
            
         
        }
    }

struct Hole1View_Previews: PreviewProvider {
    static var previews: some View {
        Hole1View()
    }
}

第 2 洞视图

// 在此视图中,您将输入各种信息,并且视图中将包含各种计算。其中一种计算是计算一个值 (Hole2totalpoints)。其中一个计算需要包含上一个视图(Hole1View)的值hole1totalpoints,以计算该视图中可以使用的2个视图(hole1totalpoints+hole2totalpoints)的总点数。

import SwiftUI

struct Hole2View: View {
    @State private var hole2gross = 0
    @State private var hole2totpoints = 0
    
    var hole2totalpoints: Int {
        let hole1points = hole2gross * 2
        return hole1points
    }
    
    
    
    var body: some View {
        
       
        
        
        Stepper("\(hole2gross)",value: $hole2gross,design: .rounded))
            .minimumScaleFactor(0.6)
            .multilineTextAlignment(.leading)
            .lineLimit(2)
        
        Text("Calculated Value \(hole2totalpoints)")
        
        
    }
}

struct Hole2View_Previews: PreviewProvider {
    static var previews: some View {
        Hole2View()
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)