有没有一种方法可以制作将您发送到Swift UI中不同UIViews的按钮

时间:2019-11-12 00:20:00

标签: swift swiftui

基本上,我需要创建一个带有嵌套if / else语句的按钮,以根据选择器的选择将用户发送到不同的UIviews。 我想在If语句中链接到UIViews,但是我不知道该怎么做。 我已经尝试过导航视图(可能是错误的),而我在Google上所做的任何操作都没有帮助


Button(action: {
      if (self.notations[self.notation]) == "Select" {

       } else if (self.instruments[self.instrument]) == "Select" {

       } else if (self.notations[self.notation]) == "Note Names" {
              if (self.instruments[self.instrument]) == "Piano" {

              } else if (self.instruments[self.instrument]) == "Guitar" {

              } else if (self.instruments[self.instrument]) == "Saxophone" {

              } else if (self.instruments[self.instrument]) == "Bass" {

              }
       } else if (self.notations[self.notation]) == "Treble Clef" {
              if (self.instruments[self.instrument]) == "Piano" {

              } else if (self.instruments[self.instrument]) == "Guitar" {

              } else if (self.instruments[self.instrument]) == "Saxophone" {

              } else {

              }
       } else if (self.notations[self.notation]) == "Bass Clef" {
              if (self.instruments[self.instrument]) == "Piano" {

              } else if (self.instruments[self.instrument]) == "Bass" {

              } else {

              }
       } else if (self.notations[self.notation]) == "TAB" {
              if (self.instruments[self.instrument]) == "Guitar" {

              } else if (self.instruments[self.instrument]) == "Bass" {

              } else {

              }
       } else if (self.notations[self.notation]) == "Piano" {
              if (self.instruments[self.instrument]) == "Guitar" {

              } else if (self.instruments[self.instrument]) == "Saxophone" {

              } else if (self.instruments[self.instrument]) == "Bass" {

               } else {

               }
       } else if (self.notations[self.notation]) == "Guitar" {
               if (self.instruments[self.instrument]) == "Saxophone" {

               } else if (self.instruments[self.instrument]) == "Piano" {

               } else {

               }
       }
}) ***

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码,并根据自己的目标进行解释。

            struct WebLinkView: View {

                @State private var notations: [String] = ["Select", "Note Names","Treble Clef", "Bass Clef", "Tab", "Piano","Guitar"];
                @State private var instruments: [String] = ["Select", "Piano","Guitar", "Saxophone", "Bass", "Bass","Guitar"];
                @State private var  notation: Int = 0
                @State private var instrument: Int = 0
                @State private var  notation_instrument: String?


                var body: some View {


                    func target<T> (_ s: T)-> NavigationLink<Text,  T> {
                       return  NavigationLink<Text, T>("New Link: " + self.notations[self.notation] + "__" + self.instruments[self.instrument], destination: s, tag: self.notations[self.notation] + self.instruments[self.instrument], selection: $notation_instrument)


                    }

                     let nextPage = target(Text("\(self.notations[self.notation]): \(self.instruments[self.instrument])"))

                     let ExamplePage1 = target(Group{VStack{Text("11")
                                   Text("Test Page")
                           Image(systemName: "house")
                           Image(systemName: "camera")
                        }})

                      let ExamplePage2 = target( Image(systemName: "heart.fill"))

                return    NavigationView{

                    Group{
                        VStack{
                    Picker(selection:self.$notation , label: Text("notation")) {
                        ForEach(0..<(notations.count)){ Text(self.notations[$0]).tag($0)  }
                    }

                    Picker(selection:self.$instrument , label: Text("instrument")){
                        ForEach(0..<(instruments.count)){ Text(self.instruments[$0]).tag($0)  }
                           }


                                    if (self.notations[self.notation]) == "Select" {
                   nextPage
                                      } else if (self.instruments[self.instrument]) == "Select" {
                   nextPage
                                      } else if (self.notations[self.notation]) == "Note Names" {
                                          if (self.instruments[self.instrument]) == "Piano" {
                   ExamplePage1
                                          } else if (self.instruments[self.instrument]) == "Guitar" {
                   ExamplePage1
                                          } else if (self.instruments[self.instrument]) == "Saxophone" {
                   ExamplePage2
                                          } else if (self.instruments[self.instrument]) == "Bass" {
                   nextPage
                                          }
                                      } else if (self.notations[self.notation]) == "Treble Clef" {
                                          if (self.instruments[self.instrument]) == "Piano" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Guitar" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Saxophone" {
                   nextPage
                                          } else {
                   nextPage
                                          }
                                      } else if (self.notations[self.notation]) == "Bass Clef" {
                                          if (self.instruments[self.instrument]) == "Piano" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Bass" {
                   nextPage
                                          } else {
                   nextPage
                                          }
                                      } else if (self.notations[self.notation]) == "TAB" {
                                          if (self.instruments[self.instrument]) == "Guitar" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Bass" {
                   nextPage
                                          } else {
                   nextPage
                                          }
                                      } else if (self.notations[self.notation]) == "Piano" {
                                          if (self.instruments[self.instrument]) == "Guitar" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Saxophone" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Bass" {
                   nextPage
                                          } else {
                   nextPage
                                          }
                                      } else if (self.notations[self.notation]) == "Guitar" {
                                          if (self.instruments[self.instrument]) == "Saxophone" {
                   nextPage
                                          } else if (self.instruments[self.instrument]) == "Piano" {
                   nextPage
                                          } else {
                   nextPage
                                          }
                                      }
                       }

                    }
                    }
                    }
                }