ของขวัญจอภาพจากพื้นฐาน 2 ค่า

0

คำถาม

ฉันมี 2 Integers:Xcode และ Ycode. พวกนี้เป็น bindings อกจากก่อนหน้าจอ ตอนนี้สิ่งที่ฉันต้องการคือให้ของขวัญเป็นใหม่ของมุมมองจากพวกนี้ integers.

แอพฯเป็นเรื่องเล็กทำแบบทดสอบอยู่ ดังนั้น 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

คุณสามารถใช้เรื่องวิธีการใช้ tuple เป็นเปลี่และบางคน 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

ContentView กับ 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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่