SwiftUI - ला चर एक पूर्णांक मूल्य के लिए अन्य दर्शनों की संख्या

0

सवाल

मैं एक छोटे से प्रश्नोत्तरी है । जब आप एक बटन दबाएँ एक विशेष राशि के लिए जोड़ा गया है Xcode और Ycode. अब मैं लेने के लिए चाहते हैं के मूल्य Xcode और Ycode मेरे साथ करने के लिए अगले देखने के लिए. तो जब आप कर रहे हैं अगले विचार आप का जवाब कर सकते हैं एक और सवाल और एक राशि में जोड़ा जाता है के शीर्ष पर मौजूदा राशि और फिर मैं चाहता हूँ लेने के लिए है कि मूल्य के लिए अभी तक एक और दृश्य है ।

तो कैसे कर सकता है मैं ले के मूल्य Xcode और Ycode करने के लिए अगले देखने के? इस कोड मैं बनाया:

    @State var ShowButton: Bool = false
    @State var ButtonYes: Bool = false
    @State var ButtonNo: Bool = false
    @State var ButtonSometimes: Bool = false
    @State var Xcode = 0
    @State var Ycode = 0

        
var body: some View {
    
        ZStack{
        Image("Chimps")
            .resizable()
            .ignoresSafeArea()
            .navigationBarHidden(true)
            
            VStack{
                Text("Question 1")
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                    .padding()
                    .foregroundColor(.white)
                    .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                    
                Spacer()
                Text(String(Xcode))
                Text(String(Ycode))
                Text("Question 1")
                    .foregroundColor(Color.white)
                    .font(.headline)
                    .padding()
                    .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                Spacer()
                Spacer()
                
                HStack(spacing:10) {
                    
                    
                Button("Yes") {
                    ShowButton = true
                    ButtonYes.toggle()
                    if ButtonYes == true {
                    Xcode += 5
                        }
                    if ButtonYes == true {
                    Ycode += 5
                        }
                    if ButtonYes == false {
                        Xcode -= 5
                        }
                    if ButtonYes == false {
                        Ycode -= 5
                        }
                    if ButtonNo == true {
                        Xcode -= 3
                    }
                    if ButtonNo == true {
                        Ycode -= 3
                    }
                    if ButtonSometimes == true {
                        Xcode -= 1
                        }
                    if ButtonSometimes == true {
                        Ycode -= 1
                        }
                    if ButtonYes == true {
                        ButtonNo = false
                    }
                    if ButtonYes == true {
                        ButtonSometimes = false
                    }
                }
                .frame(width: 50, height: 50, alignment: .center)
                .foregroundColor(.white)
                .padding()
                .background(ButtonYes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                .cornerRadius(20)
                .shadow(color: .black, radius: 10, x: 10, y: 10)
                   
                    Button("No") {
                        ShowButton = true
                        ButtonNo.toggle()
                        if ButtonNo == true {
                        Xcode += 3
                            }
                        if ButtonNo == true {
                        Ycode += 3
                            }
                        if ButtonNo == false {
                            Xcode -= 3
                            }
                        if ButtonNo == false {
                            Ycode -= 3
                            }
                        if ButtonYes == true {
                            Xcode -= 5
                        }
                        if ButtonYes == true {
                            Ycode -= 5
                        }
                        if ButtonSometimes == true {
                            Xcode -= 1
                            }
                        if ButtonSometimes == true {
                            Ycode -= 1
                            }
                        if ButtonNo == true {
                            ButtonYes = false
                        }
                        if ButtonNo == true {
                            ButtonSometimes = false
                        }
                        
                    }
                    .frame(width: 50, height: 50, alignment: .center)
                    .foregroundColor(.white)
                    .padding()
                    .background(ButtonNo ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                    .shadow(color: .black, radius: 10, x: 10, y: 10)
                    
                    Button("Sometimes") {
                        ShowButton = true
                        ButtonSometimes.toggle()
                        if ButtonSometimes == true {
                        Xcode += 1
                            }
                        if ButtonSometimes == true {
                        Ycode += 1
                            }
                        if ButtonSometimes == false {
                            Xcode -= 1
                            }
                        if ButtonSometimes == false {
                            Ycode -= 1
                            }
                        if ButtonYes == true {
                            Xcode -= 5
                        }
                        if ButtonYes == true {
                            Ycode -= 5
                        }
                        if ButtonNo == true {
                            Xcode -= 3
                            }
                        if ButtonNo == true {
                            Ycode -= 3
                            }
                        if ButtonSometimes == true {
                            ButtonYes = false
                        }
                        if ButtonSometimes == true {
                            ButtonNo = false
                        }
                      
                        
                    }
                    .frame(width: 50, height: 50, alignment: .center)
                    .foregroundColor(.white)
                    .padding()
                    .background(ButtonSometimes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                    .shadow(color: .black, radius: 10, x: 10, y: 10)
                }
                
                Spacer()
                if ShowButton {
                NavigationLink(
                    destination: Question2(),
                
                    label: {
                Rectangle()
                            .fill(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .frame(width: 150, height: 80, alignment: .bottom)
                    .cornerRadius(20)
                    .padding(10)
                    .shadow(color: .black, radius: 10, x: 10, y: 10)
                        
                    .overlay(
                        Text("Question 2")
                            .font(.largeTitle)
                            .foregroundColor(.white)
                            .shadow(color: .black, radius: 10, x: 10, y: 10)
                    )}
                
            )
                Spacer()
            }
    }
        
    
}
           
}
}

integer swift swiftui view
2021-11-22 14:35:46
1

सबसे अच्छा जवाब

1

यहाँ है एक त्वरित और काम के उदाहरण के कैसे आप का उपयोग कर सकता है @बाध्यकारी दूसरी स्क्रीन पर अद्यतन करने के लिए मौजूदा राशि. लेकिन मैं आप की सिफारिश करने के लिए पर एक नज़र रखना डॉक्स और समझते हैं जब उपयोग करने के लिए @राज्य, @बाध्यकारी @ObservedObject, @StateObject, आदि.

import SwiftUI

struct FirstView: View {
    
    @State var xValue = 0
    @State var yValue = 0
           
    var body: some View {
        NavigationView {
            VStack {
                Text("\(xValue)")
                Text("\(yValue)")
                Button("add to x") {
                    xValue += 1
                }
                Button("add to y") {
                    yValue += 1
                }
                Divider()
                NavigationLink("next screen", destination: SecondView(xValue: $xValue, yValue: $yValue))
            }
        }
    }

}

struct SecondView: View {
    
    @Binding var xValue: Int
    @Binding var yValue: Int
    
    var body: some View {
        NavigationView {
            VStack {
                Text("\(xValue)")
                Text("\(yValue)")
                Button("add to x") {
                    xValue += 1
                }
                Button("add to y") {
                    yValue += 1
                }
            }
        }
    }
}
2021-11-22 18:48:25

बहुत बहुत धन्यवाद के लिए समय लेने के लिए मेरे सवाल का जवाब. मैं देख रहा हूँ कई वीडियो और पढ़ने के बारे में बाइंडिंग और अन्य वस्तुओं आप उल्लेख किया है । मैं स्वीकार किए जाते हैं आप का जवाब और upvoted । फिर से, धन्यवाद :)
Curiousnoes

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................