繁体   English   中英

SwiftUI 在一种情况下而不是另一种情况下查看层次结构警告

[英]SwiftUI View Hierarchy Warning in One Case and Not Another

我最初制作了一个 SwiftUI 列表/详细信息应用程序,其中包括一个带有一个选项卡的 TabView 作为添加新记录的视图。 这和预期的差不多。 我想将选项卡中的添加功能更改为从导航栏按钮调用。 在这两种情况下,我都使用完全相同的视图。 当从导航栏按钮调用视图时,我得到 tableView 层次结构错误:

[TableView] 仅警告一次:UITableView 被告知布局其可见单元格和其他内容,而不是在视图层次结构中(表视图或其超级视图之一尚未添加到窗口中)....+ 更多喋喋不休

TabView 调用:

AddNewPatient().tabItem ({
    Image(systemName: "plus.circle")
    Text("Add")
})
.tag(2)

导航按钮调用:

.navigationBarItems(trailing: NavigationLink(destination: AddNewPatient()) {
    Text("Add Patient")
})      

我很困惑为什么从导航栏按钮调用视图时视图超出层次结构。 到目前为止,我还没有遇到过应用程序崩溃,但显然应用程序不开心。 数据确实已保存并显示在列表中。 (核心数据存储)

我在 UITableViewAlertForLayoutOutsideViewHierarchy 设置了一个符号断点,但我所能收集到的只是相同的数据——在视图层次结构之外。

Xcode 11.1 (11A1027) iOS 13.1.2

任何指导将不胜感激。

AddNewPatient 视图:

import SwiftUI
import CoreData

struct AddNewPatient: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.presentationMode) var presentationMode
        
    @State private var updatedTitle: String = "No Title"
    @State private var updatedFirstName: String = "No First Name"
    //more properties
    
    @State var photos: [UIImage] = [UIImage]()
    @State private var showImagePicker: Bool = false
    
    var bImage: Image = Image("InventoryThinBlueWithCamera")

    var body: some View {
        NavigationView {
            ScrollView {
            VStack {
                if photos.count > 0 {
                    Image(uiImage: photos[photos.count - 1])
                        .resizable()
                        .frame(width: 320, height: 320)
                        .aspectRatio(contentMode: .fit)
                        .cornerRadius(12)
                } else {
                    bImage
                }

                VStack {
                Button("Take Photo") {
                    self.showImagePicker = true
                }
                .padding()
                .background(Color.green)
                .foregroundColor(.white)
                .cornerRadius(12)
                //.disabled(!showAlert)
                }
                .sheet(isPresented: self.$showImagePicker) {
                    PhotoCaptureView(photos: self.$photos)
                }

                VStack(alignment: .leading) {
                    Text("Patient Title:")
                        .padding(.leading, 5)
                        .font(.headline)
                    TextField("Enter a Title", text: $updatedTitle)
                        .onAppear {
                            self.updatedTitle = ""
                    }
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                }
                .padding(10)
                
                
                VStack(alignment: .leading) {
                    Text("First Name:")
                        .padding(.leading, 5)
                        .font(.headline)
                    TextField("Enter First Name", text: $updatedFirstName)
                        .onAppear {
                            self.updatedFirstName = ""
                    }
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                }
                .padding(10)
                
                //bunch more fields
                

                
                Spacer()
            }
        }//ScrollView
        .navigationBarTitle("Add New Patient", displayMode: .inline)
        .padding(.top, 50)
        .navigationBarItems(trailing:  Button(action: ({
                    
            let nmo = Patient(context: self.managedObjectContext)
            var chosenImageData : Data?

            if self.photos.count > 0 {
                let theImage = self.photos[self.photos.count - 1]
                
                let rir = CRSReduceImageResolution()
                let temp = rir.resizeMyImageData(theImage, startSize: CGSize.zero, endSize: CGSize(width: 320, height: 240))
                chosenImageData = temp
                //chosenImageData = myImageData
                nmo.image = chosenImageData
            }

            nmo.myID = UUID()
            nmo.title = self.updatedTitle
            nmo.firstName = self.updatedFirstName
            nmo.lastName = self.updatedLastName
            nmo.createdAt = Date()
            
            do {
                try self.managedObjectContext.save()
            } catch {
                print(error)
            }
                    
            self.photos = []
                    self.updatedTitle = ""
                    self.updatedFirstName = ""
                    //bunch more fields
                })) {
                    Text("Save")
                })
        }//nav
    }
}

我更改了尾随按钮调用以切换属性以显示添加患者表,然后调用 function 来添加患者。 这完美地工作。

 .navigationBarTitle("Person")
 .sheet(isPresented: $showAddPatient) {
      AddNewPatient(photoStore: self.photoStore)
  }

  .navigationBarItems(
       leading: EditButton(),
       trailing: Button(
           action: {
               withAnimation { self.showAddPatient.toggle() }
           }
        ) {
          Image(systemName: "plus")
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM