我遇到了一个奇怪的问题,试图在我的应用程序中实现NavigationStack{}-我认为这与我的目标有多个动态列表,他们正在更新的事实有关?
我遇到过一些相同的问题,但没有真实的的回答。当我从目标视图中删除导航堆栈时,问题就消失了。
主视图
struct TestView: View {
@StateObject var accountData = AccountData()
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
List {
Section("Accounts") {
ForEach(account) { account in
NavigationLink(value: account) {
Text(account.mainInformation.name)
}
}
}
Section("Contacts") {
ForEach(account) { account in
let contact = account.contacts
ForEach(contact) { contact in
NavigationLink(value: contact) {
Text(contact.name)
}
}
}
}
}
.navigationDestination(for: Account.self) { account in
AccountDetailView(account: binding(for: account))
}
.navigationDestination(for: Contact.self) { contact in
Text(contact.name)
}
}
.navigationTitle("Categories")
}
extension TestView {
var account: [Account] {
accountData.accounts
}
// Finding the index of a particular account in the data model array
func binding(for account: Account) -> Binding<Account> {
guard let index = accountData.index(of: account) else {
fatalError("Account not found")
}
return $accountData.accounts[index]
}
}
目标视图
struct AccountDetailView: View {
@Binding var account: Account
@State private var isPresenting = false
var body: some View {
NavigationStack {
VStack {
VStack(alignment: .leading) {
Rectangle()
.fill(.white)
.frame(height: 150)
.blur(radius: 1)
.clipShape (RoundedShape (corners: [.bottomRight])).shadow(radius: 8)
.edgesIgnoringSafeArea(.top)
.padding(.bottom, -140)
VStack(alignment: .leading) {
HStack {
Text(account.mainInformation.address)
.padding(.horizontal, 10)
.padding(.bottom, 10)
.font(.title)
.fontWeight(.semibold)
}
.padding(.bottom, -20)
Text(account.mainInformation.city)
.padding(.horizontal, 10)
.padding(.bottom, 2)
.font(.headline)
Text(account.mainInformation.number)
.foregroundColor(.blue)
.padding(.horizontal, 10)
}
Divider()
// Lists for supporting contacts and tasks
List {
Section("Contacts") {
ForEach(account.contacts.indices, id: \.self) { index in
let contact = account.contacts[index]
ContactListRow(contact: contact)
}
}
Section("Tasks") {
ForEach(account.tasks.indices, id: \.self) { index in
let task = account.tasks[index]
Text(task.action)
}
}
}
}
}
}
// Navigation title for account name - Compatability with NavStack?
.navigationTitle(account.mainInformation.name)
.background(Color(.systemGroupedBackground))
//.scrollContentBackground(.hidden)
//.edgesIgnoringSafeArea(.bottom)
// SHEET CODE for sheet that presents on edit
.toolbar {
ToolbarItem {
HStack {
Button("Edit") {
isPresenting = true
}
.foregroundColor(.black)
.fontWeight(.bold)
}
}
}
.sheet(isPresented: $isPresenting) {
NavigationStack {
ModifyAccountView(account: $account)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
isPresenting = false
}
}
}
}
}
}
}
1条答案
按热度按时间djmepvbi1#
假设您有两个
view1
和view2
。view1中绑定了变量a,并且还传递给了view2。现在,如果您更改了view2中的变量a,SwiftUI将自动将NavigationStack中的所有视图弹出到已声明该变量a的当前视图。您还将一些绑定变量传递给AccountDetailView
,您需要仔细检查是否有任何值链接在导航推送上,这是TestView
中的绑定。我已经浏览了这两个swift文件,我没有看到任何变化,但您需要浏览整个代码,并找到任何变化的值。