SwiftUI realm数据库问题将获取的数据传递到另一个视图

huangapple go评论76阅读模式
英文:

SwiftUI realm database Issue passing the fetched data to another view

问题

I'm using realm database and I had issue after fetching for the data for a list and use on tap gesture property to go to the detail view it's only show the first set of data in my realm database.

这是我使用 Realm 数据库时遇到的问题,我在获取列表数据后使用"on tap gesture"属性进入详细视图时,它只显示了 Realm 数据库中的第一组数据。

I will show some pictures to illustrate.

我将展示一些图片来进行说明。

this is how my list look like

这是我的列表的外观。

and this is detail view when clicked and it's only showing the first set of data whatever what you clicked on.

当点击详细视图时,它只显示第一组数据,无论你点击了什么。

ForEach(realmManger.patterns, id: \.id) { userPattern in
                                    
    patternCell(title: userPattern.title ?? "Error??", date: userPattern.dateCreated ?? Date.now)
    .onTapGesture {
     editPattern = true
    }.sheet(isPresented: $editPattern) {
    patternEdit(userPattern: userPattern)
      }
}

这是代码的一部分,因为它太长,我只展示了关键部分。

struct patternEdit: View {
        
        @EnvironmentObject var realmManger: RealmManger
        @State var showAlert = false
        
        let userPattern: Patterns
        
        var body: some View {
       
                    Text(userPattern.title ?? "Error")
                        .font(.title)
                        .foregroundColor(Color("lightBlue"))
                        .padding()
    
    }
}

这是详细视图的代码,为什么它只显示第一组数据?

这些是图片链接。

英文:

I'm using realm database and I had issue after fetching for the data for a list and use on tap gesture property to go to the detail view it's only show the first set of data in my realm database.
I will show some pictures to illustrate.
SwiftUI realm数据库问题将获取的数据传递到另一个视图

this is how my list look like

SwiftUI realm数据库问题将获取的数据传递到另一个视图

and this is detail view when clicked and it's only showing the first set of data whatever what you clicked on.

 ForEach(realmManger.patterns, id: \.id) { userPattern in
                                
patternCell(title: userPattern.title ?? "Error??", date: userPattern.dateCreated ?? Date.now)
.onTapGesture {
 editPattern = true
}.sheet(isPresented: $editPattern) {
patternEdit(userPattern: userPattern)
  .environmentObject(realmManger)
  }
}

the code is too long I tried to show what important

struct patternEdit: View {
    
    @EnvironmentObject var realmManger:RealmManger
    @State var showAlert = false
    
    let userPattern:Patterns
    
    var body: some View {
   
                Text(userPattern.title ?? "Error")
                    .font(.title)
                    .foregroundColor(Color("lightBlue"))
                    .padding()

}

and this is the code for the detail view why is it only showing the first data ?

答案1

得分: 2

.sheet() 移出 ForEach 循环,使用 .sheet(item: ...),就像在这个示例代码中使用 @State var selectedPattern: ... 一样。

@State var selectedPattern: Patterns?  // <-- 这里
// ...

var body: some View {
    // ...
    ForEach(realmManager.patterns, id: \.id) { userPattern in
        patternCell(title: userPattern.title ?? "Error??", date: userPattern.dateCreated ?? Date.now)
            .onTapGesture {
                selectedPattern = userPattern  // <-- 这里
            }
    }
}
.sheet(item: $selectedPattern) { userPattern in  // <-- 这里
    patternEdit(userPattern: userPattern)
        .environmentObject(realmManager)
}
// ...
}

根据你的需要调整示例代码。

英文:

Take the .sheet() out of the ForEach loop and use the .sheet(item: ...),
like in this example code using @State var selectedPattern: ...

 @State var selectedPattern: Patterns?  // <-- here
 // ...
 
 var body: some View {
     // ...
     ForEach(realmManger.patterns, id: \.id) { userPattern in
         patternCell(title: userPattern.title ?? "Error??", date: userPattern.dateCreated ?? Date.now)
             .onTapGesture {
                 selectedPattern = userPattern  // <-- here
             }
     }
     .sheet(item: $selectedPattern) { userPattern in  // <-- here
         patternEdit(userPattern: userPattern)
             .environmentObject(realmManger)
     }
 // ...
 }

Adjust the example code to suit your purpose.

huangapple
  • 本文由 发表于 2023年2月24日 09:48:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定