微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用SwiftUI

如何解决使用SwiftUI

我正在尝试在一个结构中包含两个NavigationLink,我将其与NavigationLink一起用作List内的ForEach,但是代码不起作用(导航View都不会发生,这是代码

struct mainView : View {
    @State var people = [Person(name: "one",family: "1",type: "men"),Person(name: "two",family: "2",type: "women")]
    var body : some View {
        List{
           ForEach(self.people,id:\.self){person in 
               NavigationLink(destination: Text("hi")) {
                 PersonView(person : person)
               }.buttonStyle(BorderlessButtonStyle())
        }
    }
}


struct Person {
    var name : String
    var family : String
    var type : String
}

struct PersonView : View {
    @State var person : Person?
    var body : some View {
      HStack{
        NavigationLink(destination: Text(self.person.name)) {
           Text("this is \(self.person.name) \(self.person.family)")
        }
        NavigationLink(destination: Text(self.person.type)) {
           Text(self.person.type)
        }
      }
    }
}

在阅读后我添加.buttonStyle 可能有所帮助,但是它什么也没做。我还尝试了以下PersonView:

struct PersonView : View {
@State var person : Person?
@State var goToFirst = false
@State var goToType = false
var body : some View {
   vstack{
      Button(action:{
         self.goToFirst.toggle()
      }){
         Text("this is \(self.person.name) \(self.person.family)")
      }.buttonStyle(BorderlessButtonStyle())
      Button(action:{
         self.goToType.toggle()
      }){
         Text(self.person.type)
      }.buttonStyle(BorderlessButtonStyle())
      NavigationLink(destination: Text(self.person.name),isActive : self.$goToFirst) {
           Text("").frame(width: 0.01,height: 0.01)
      }
      NavigationLink(destination: Text(self.person.type),isActive : self.$goToType) {
           Text("").frame(width: 0.01,height: 0.01)
      }
   }
}

效果不佳,我应该怎么做?当我单击单元格上的任意位置时,它将导航到Text("hi") ;当我单击相应区域时,它将导航至正确的{{1 }}。 重要提示:此视图正从View

包装的视图导航到

解决方法

您尚未添加navigationView,就是问题所在。

struct MainView: View {
    @State private var peoples = [Person(name: "one",family: "1",type: "men"),Person(name: "two",family: "2",type: "women")]
    var body: some View {
        NavigationView { //This is important
            List {
                ForEach(self.peoples,id: \.name) { people in
                    NavigationLink(destination: PersonView(person: people)) {
                        Text(people.name)
                    }
                }
            }
            .navigationTitle("Demo")
        }
    }
}

struct Person {
    var name : String
    var family : String
    var type : String
}

struct PersonView : View {
    @State var person : Person?
    var body : some View {
      HStack{
        NavigationLink(destination: Text(self.person.name)) {
           Text("this is \(self.person.name) \(self.person.family)")
        }
        NavigationLink(destination: Text(self.person.type)) {
           Text(self.person.type)
        }
      }
    }
}
,

我认为以下是您想要的。可以将代码粘贴到一个空项目中。

一些说明:

List似乎使事情变得更加困难,因此我将其替换为VStack。您可能需要用ScrollView包围它。

同样,正如用户Asperi所暗示的那样,当您创建多个NavigationLink但围绕一个EmptyView之后又将其激活时,此方法效果最佳。

struct MainView: View {
    @State var people = [Person(name: "one",type: "women")]

    var body: some View {
        VStack {
            ForEach(self.people,id: \.self) { person in
                PersonView(person: person)
            }
        }
    }
}

struct Person: Hashable {
    var name: String
    var family: String
    var type: String
}

struct PersonView: View {
    let person: Person
    @State private var selection: Int?
    
    var body: some View {
        HStack {
            NavigationLink(
                destination: Text(self.person.name),tag: self.person.hashValue + 1,selection: self.$selection) {

                EmptyView()
            }
            NavigationLink(
                destination: Text(self.person.type),tag: self.person.hashValue + 2,selection: self.$selection) {
                
                EmptyView()
            }
            
            Button("this is \(self.person.name) \(self.person.family)") {
                self.selection = self.person.hashValue + 1
            }
            Spacer()
            Button(self.person.type) {
                self.selection = self.person.hashValue + 2
            }.padding()
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            MainView()
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。