현재 화면에 따라 값 2

0

질문

저는 2 정수:Xcode 및 Ycode. 이들은 바인딩에서 이전 화면이 있습니다. 지금 내가 원하는 것은 현재의 새로운 볼에 따라 이러한 정수입니다.

응용 프로그램은 작은 퀴즈가 있습니다. 그래서 Xcode 및 Ycode 은 점수를 제공합니다. 그러나 나는 선물하고 싶은 새로운 전망을 클릭하면"버튼을 클릭하"나를 기반으로 Xcode 및 Ycode.

예를 들어:

Value X = between 8-15 and value Y = between 8-23 -> present screen1
Value X = between 8-15 and value Y = between 24-40 -> present screen2
Value X = between 16-23 and value Y = between 8-17 -> present screen3
And so on......

This is my Code:
      @Binding var Xcode: Int
        @Binding var Ycode: Int
        @State var ShowButton: Bool = false
        @State var ButtonYes: Bool = false
        @State var ButtonNo: Bool = false
        @State var ButtonSometimes: Bool = false

    
    var body: some View {
        ZStack{
        Image("Zebras")
            .resizable()
            .ignoresSafeArea()
            .navigationBarHidden(true)
            
            
            VStack{
                Text("Wat ben ik?")
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                    .padding()
                    .foregroundColor(.white)
                    .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                Spacer()
                Text("Je heb alle vragen beantwoord. Nu is de vraag: Welk dier ben ik?")
                    .foregroundColor(Color.white)
                    .font(.headline)
                    .padding()
                    .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                Spacer()
                
                Text("Your score:")
                    .foregroundColor(Color.white)
                    .font(.headline)
                    .padding()
                    .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                
                HStack (spacing:0){
                    Text("X = ")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .padding()
                        .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                        .cornerRadius(20)
                    
                Text(String(Xcode))
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .padding()
                    .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                }
                HStack (spacing:0){
                                        
                    Text("Y = ")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .padding()
                        .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                        .cornerRadius(20)
                    
                Text(String(Ycode))
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .padding()
                        .background(Color(red: 0.493, green: 0.184, blue: 0.487))
                        .cornerRadius(20)
                }
                Spacer()
                
                
                
                Button("Click here!") {
                    
                    
                }
                    
                .frame(width: 100, height: 50, alignment: .center)
                .font(.headline)
                .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)
                Spacer()
                
            }
        }
    }
}

어떻게 만들까요?

integer screen swift swiftui
2021-11-23 11:05:13
3

최고의 응답

2

당신이 사용할 수 있는 이 방법을 사용하여,튜플 스위치고 일부 NavigationLinks:

struct ContentView: View {
    @State var xy = (5.0,7.0) // <-- use a tuple
    
    var body: some View {
        NavigationView {
            QuizView(xy: $xy)
        }.navigationViewStyle(.stack)
    }
}

struct QuizView: View {
    @Binding var xy: (Double, Double)
    
    // set of ranges of your scores for each screen
    let screen1X = 1.0..<4.0
    let screen1Y = 2.0..<4.0
    
    let screen2X = 3.0..<4.0
    let screen2Y = 4.0..<8.0
    
    let screen3X = 5.0..<9.0
    let screen3Y = 6.0..<8.0
    
    @State private var action: Int? = 0
    
    var body: some View {
        VStack {
            Button(action: {
                switch xy {
                case (screen1X,screen1Y): action = 1
                case (screen2X,screen2Y): action = 2
                case (screen3X,screen3Y): action = 3
                default:
                    print("---> default")
                }
            }) {
                Text("Click me")
            }
            NavigationLink(destination: Text("screen1X"), tag: 1, selection: $action) {EmptyView()}
            NavigationLink(destination: Text("screen2X"), tag: 2, selection: $action) {EmptyView()}
            NavigationLink(destination: Text("screen3X"), tag: 3, selection: $action) {EmptyView()}
        }
    }
}
2021-11-23 12:57:46

당신을 감사하므로 많은 시간을 내 질문에 대답! 이것은 또한 내가 무엇을 찾고 있었다. 많은 것을 배웠다는 이 대답,당신을 감사하십시오!
Curiousnoes
1

콘텐츠와 2 개의 값:valueX 및 valueY. 다음 범위에 당신은 필요합니다. 다음 계산된 속성을 결정하는 화면 이동합니다. 는 버튼을 만들 경우에는 계산 시 반환하지 않 nil. 그리고 목적지의 링크가 스위치를 결정 하는 화면이 표시 및 라벨는 버튼을 클릭합니다.

import SwiftUI

struct MContentView: View {
    @State var valueX = 17
    @State var valueY = 15
    
    @State var isNextViewActive = false
    
    private let range8_15 = 8...15
    private let range16_23 = 16...23
    private let range8_23 = 8...23
    private let range24_40 = 24...40
    private let range8_17 = 8...17
    
    private var screenToPresent: Int? {
        if range8_15.contains(valueX) && range8_23.contains(valueY) {
            return 1
        } else if range8_15.contains(valueX) && range24_40.contains(valueY) {
            return 2
        } else if range16_23.contains(valueX) && range8_17.contains(valueY) {
            return 3
        }
        
        return nil
    }
    
    var body: some View {
        NavigationView {
            if let screen = self.screenToPresent {
                NavigationLink(isActive: self.$isNextViewActive, destination: {
                    switch screen {
                    case 1:
                        MView1()
                    case 2:
                        MView2()
                    case 3:
                        MView3()
                    default:
                        EmptyView()
                    }
                }) {
                    Button(action: {
                        self.isNextViewActive = true
                    }) {
                        Text("Click me!")
                    }
                }
            }
        }
    }
}

struct MView1: View {
    var body: some View {
        Text("My View 1")
    }
}

struct MView2: View {
    var body: some View {
        Text("My View 2")
    }
}

struct MView3: View {
    var body: some View {
        Text("My View 3")
    }
}

struct MExample_Previews: PreviewProvider {
    static var previews: some View {
        MContentView()
    }
}

희망은 당신이 무엇을 찾고 있다!

2021-11-23 13:19:55

감사합니다! 이것은 무엇을 찾고 있었다. 당신은 시간을 응답 내 질문:)
Curiousnoes

당신은 환영합니다!
Alexander Thoren
1

할 수 있습이 숨겨져 있 는 탐색의 링크 이와 같은:

NavigationLink(destination: View1(), isActive: $condition1, label: { EmptyView() })
NavigationLink(destination: View2(), isActive: $condition2, label: { EmptyView() })
NavigationLink(destination: View3(), isActive: $condition3, label: { EmptyView() })
// note that having empty views for your links will keep them hidden on your layout

각각에 대한 X 와 Y 조건입니다.

그래서,당신은 확인을 위한 X 와 Y 값을 확인할 수 있습들을 다음과 같다:

condition1 = Xcode >= 8 && Xcode <= 15 && Ycode >= 8 && Code <= 23
condition2 = Xcode >= 8 && Xcode <= 15 && Ycode >= 24 && Code <= 40
condition3 = Xcode >= 16 && Xcode <= 23 && Ycode >= 8 && Code <= 17

그리고 활성화하려는 링크와 현재 화면 당신은 필요합니다.

2021-11-23 12:43:33

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................