如何在另一个 NavigationLink 中有一个 NavigationLink
How to have a NavigationLink inside another NavigationLink
我和我的团队目前正在 SwiftUI 中开发 Mastodon 客户端,我有一个简单的 StatusView,我可以在其中显示所有 post 数据,目前看起来像这样:
这个视图在我的项目中称为 StatusView
,有两个 NavigationLinks
:主要的一个,将用户重定向到 post 的线程,另一个重定向点击 post 的个人资料图片时,用户会转到 post 的作者个人资料。
到这里为止,一切正常。如果您点击 post 上除按钮(点赞、提升和分享)或个人资料图片以外的任何位置,它会打开话题。如果您点击个人资料图片,它会打开作者的个人资料。
但是如果你点击下面个人资料图片,应用程序崩溃,只给我以下错误:
2020-08-23 21:32:39.929392-0400 Hyperspace[830:147862] WF: _WebFilterIsActive returning: NO
2020-08-23 21:32:40.117865-0400 Hyperspace[830:147862] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}>
2020-08-23 21:32:40.117994-0400 Hyperspace[830:147862] [ProcessSuspension] 0x11decfa80 - ProcessAssertion: Failed to acquire RBS Background assertion 'WebProcess Background Assertion' for process with PID 837, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}
2020-08-23 21:32:40.119401-0400 Hyperspace[830:147862] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}>
2020-08-23 21:32:40.119549-0400 Hyperspace[830:147862] [ProcessSuspension] 0x11decfac0 - ProcessAssertion: Failed to acquire RBS Suspended assertion 'WebProcess Suspended Assertion' for process with PID 837, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}
Fatal error: UIKitNavigationBridge: multiple active destinations: file SwiftUI, line 0
2020-08-23 21:32:40.292135-0400 Hyperspace[830:147862] Fatal error: UIKitNavigationBridge: multiple active destinations: file SwiftUI, line 0
我想这是因为当你点击那里时,线程和个人资料的导航链接都会被触发,导致应用程序崩溃,因为有多个活动目的地,因为它试图转到线程和个人资料同时
TL;DR
我的应用程序崩溃是因为两个 NavigationLinks 在不应该同时被触发。
我该如何解决这个问题?
提前致谢。
我的代码
/// The status is being displayed in a ``StatusList``, so we should make it smaller and more compact.
private struct CompactStatusView: View {
/// The ``Status`` data model from where we obtain all the data.
var status: Status
/// Used to trigger the navectigationLink to redirect the user to the thread.
@Binding var goToThread: Bool
/// Used to redirect the user to a specific profile.
@Binding var profileViewActive: Bool
var body: some View {
ZStack {
self.content
.padding(.vertical, 5)
.contextMenu(
ContextMenu(menuItems: {
Button(action: {}, label: {
Label("Report post", systemImage: "flag")
})
Button(action: {}, label: {
Label("Report \(self.status.account.displayName)", systemImage: "flag")
})
Button(action: {}, label: {
Label("Share as Image", systemImage: "square.and.arrow.up")
})
})
)
}
.buttonStyle(PlainButtonStyle())
.navigationBarHidden(self.profileViewActive)
}
var content: some View {
HStack(alignment: .top, spacing: 12) {
URLImage(URL(string: self.status.account.avatarStatic)!,
placeholder: { _ in
Image("amodrono")
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
.redacted(reason: .placeholder)
},
content: {
[=11=].image
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
}
)
.onTapGesture {
self.profileViewActive.toggle()
}
.background(
NavigationLink(
destination: ProfileView(
accountInfo: ProfileViewModel(
accountID: self.status.account.id
),
isParent: false
),
isActive: self.$profileViewActive
) {
Text("")
}
.frame(width: 0, height: 0)
)
VStack(alignment: .leading, spacing: 2) {
HStack(alignment: .firstTextBaseline) {
if !self.status.account.displayName.isEmpty {
Text("\(self.status.account.displayName)")
.font(.headline)
.lineLimit(1)
}
Text("@\(self.status.account.acct)")
.foregroundColor(.secondary)
.lineLimit(1)
Text("· \(self.status.createdAt.getDate()!.getInterval())")
.foregroundColor(.secondary)
.lineLimit(1)
}
StatusViewContent(
isMain: false,
content: self.status.content,
card: self.status.card,
attachments: self.status.mediaAttachments,
goToProfile: self.$profileViewActive
)
StatusActionButtons(
isMain: false,
repliesCount: self.status.repliesCount,
reblogsCount: self.status.reblogsCount,
favouritesCount: self.status.favouritesCount,
statusUrl: self.status.uri
)
}
.onTapGesture {
self.goToThread.toggle()
}
.background(
NavigationLink(
destination: ThreadView(
mainStatus: self.status
),
isActive: self.$goToThread
) {
EmptyView()
}
)
Spacer()
}
}
}
我想这里的重要部分是:
头像:
URLImage(URL(string: self.status.account.avatarStatic)!,
placeholder: { _ in
Image("amodrono")
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
.redacted(reason: .placeholder)
},
content: {
[=12=].image
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
}
)
.onTapGesture {
self.profileViewActive.toggle()
}
.background(
NavigationLink(
destination: ProfileView(
accountInfo: ProfileViewModel(
accountID: self.status.account.id
),
isParent: false
),
isActive: self.$profileViewActive
) {
Text("")
}
.frame(width: 0, height: 0)
)
最后两个修饰符
.onTapGesture {
self.goToThread.toggle()
}
.background(
NavigationLink(
destination: ThreadView(
mainStatus: self.status
),
isActive: self.$goToThread
) {
EmptyView()
}
)
这里是一些复制场景的可能解决方案的演示(因为提供的代码不可测试 as-is)。这个想法是重复使用一个 NavigationLink,但根据激活位置使用不同的目的地。
测试 Xcode 12 / iOS 14
struct DemoRowView: View {
@State private var isProfile = false
@State private var isActive = false
var body: some View {
HStack {
Image(systemName: "person")
.scaledToFit()
.onTapGesture {
self.isProfile = true
self.isActive = true
}
Text("Thread description")
.onTapGesture {
self.isProfile = false
self.isActive = true
}
.background(
NavigationLink(destination: self.destination(), isActive: $isActive) { EmptyView() }
)
}
.buttonStyle(PlainButtonStyle())
}
@ViewBuilder
private func destination() -> some View {
if isProfile {
ProfileView()
} else {
ThreadView()
}
}
}
我和我的团队目前正在 SwiftUI 中开发 Mastodon 客户端,我有一个简单的 StatusView,我可以在其中显示所有 post 数据,目前看起来像这样:
这个视图在我的项目中称为 StatusView
,有两个 NavigationLinks
:主要的一个,将用户重定向到 post 的线程,另一个重定向点击 post 的个人资料图片时,用户会转到 post 的作者个人资料。
到这里为止,一切正常。如果您点击 post 上除按钮(点赞、提升和分享)或个人资料图片以外的任何位置,它会打开话题。如果您点击个人资料图片,它会打开作者的个人资料。
但是如果你点击下面个人资料图片,应用程序崩溃,只给我以下错误:
2020-08-23 21:32:39.929392-0400 Hyperspace[830:147862] WF: _WebFilterIsActive returning: NO
2020-08-23 21:32:40.117865-0400 Hyperspace[830:147862] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}>
2020-08-23 21:32:40.117994-0400 Hyperspace[830:147862] [ProcessSuspension] 0x11decfa80 - ProcessAssertion: Failed to acquire RBS Background assertion 'WebProcess Background Assertion' for process with PID 837, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}
2020-08-23 21:32:40.119401-0400 Hyperspace[830:147862] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}>
2020-08-23 21:32:40.119549-0400 Hyperspace[830:147862] [ProcessSuspension] 0x11decfac0 - ProcessAssertion: Failed to acquire RBS Suspended assertion 'WebProcess Suspended Assertion' for process with PID 837, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}
Fatal error: UIKitNavigationBridge: multiple active destinations: file SwiftUI, line 0
2020-08-23 21:32:40.292135-0400 Hyperspace[830:147862] Fatal error: UIKitNavigationBridge: multiple active destinations: file SwiftUI, line 0
我想这是因为当你点击那里时,线程和个人资料的导航链接都会被触发,导致应用程序崩溃,因为有多个活动目的地,因为它试图转到线程和个人资料同时
TL;DR
我的应用程序崩溃是因为两个 NavigationLinks 在不应该同时被触发。我该如何解决这个问题?
提前致谢。
我的代码
/// The status is being displayed in a ``StatusList``, so we should make it smaller and more compact.
private struct CompactStatusView: View {
/// The ``Status`` data model from where we obtain all the data.
var status: Status
/// Used to trigger the navectigationLink to redirect the user to the thread.
@Binding var goToThread: Bool
/// Used to redirect the user to a specific profile.
@Binding var profileViewActive: Bool
var body: some View {
ZStack {
self.content
.padding(.vertical, 5)
.contextMenu(
ContextMenu(menuItems: {
Button(action: {}, label: {
Label("Report post", systemImage: "flag")
})
Button(action: {}, label: {
Label("Report \(self.status.account.displayName)", systemImage: "flag")
})
Button(action: {}, label: {
Label("Share as Image", systemImage: "square.and.arrow.up")
})
})
)
}
.buttonStyle(PlainButtonStyle())
.navigationBarHidden(self.profileViewActive)
}
var content: some View {
HStack(alignment: .top, spacing: 12) {
URLImage(URL(string: self.status.account.avatarStatic)!,
placeholder: { _ in
Image("amodrono")
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
.redacted(reason: .placeholder)
},
content: {
[=11=].image
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
}
)
.onTapGesture {
self.profileViewActive.toggle()
}
.background(
NavigationLink(
destination: ProfileView(
accountInfo: ProfileViewModel(
accountID: self.status.account.id
),
isParent: false
),
isActive: self.$profileViewActive
) {
Text("")
}
.frame(width: 0, height: 0)
)
VStack(alignment: .leading, spacing: 2) {
HStack(alignment: .firstTextBaseline) {
if !self.status.account.displayName.isEmpty {
Text("\(self.status.account.displayName)")
.font(.headline)
.lineLimit(1)
}
Text("@\(self.status.account.acct)")
.foregroundColor(.secondary)
.lineLimit(1)
Text("· \(self.status.createdAt.getDate()!.getInterval())")
.foregroundColor(.secondary)
.lineLimit(1)
}
StatusViewContent(
isMain: false,
content: self.status.content,
card: self.status.card,
attachments: self.status.mediaAttachments,
goToProfile: self.$profileViewActive
)
StatusActionButtons(
isMain: false,
repliesCount: self.status.repliesCount,
reblogsCount: self.status.reblogsCount,
favouritesCount: self.status.favouritesCount,
statusUrl: self.status.uri
)
}
.onTapGesture {
self.goToThread.toggle()
}
.background(
NavigationLink(
destination: ThreadView(
mainStatus: self.status
),
isActive: self.$goToThread
) {
EmptyView()
}
)
Spacer()
}
}
}
我想这里的重要部分是:
头像:
URLImage(URL(string: self.status.account.avatarStatic)!,
placeholder: { _ in
Image("amodrono")
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
.redacted(reason: .placeholder)
},
content: {
[=12=].image
.resizable()
.scaledToFit()
.clipShape(Circle())
.frame(width: 50, height: 50)
}
)
.onTapGesture {
self.profileViewActive.toggle()
}
.background(
NavigationLink(
destination: ProfileView(
accountInfo: ProfileViewModel(
accountID: self.status.account.id
),
isParent: false
),
isActive: self.$profileViewActive
) {
Text("")
}
.frame(width: 0, height: 0)
)
最后两个修饰符
.onTapGesture {
self.goToThread.toggle()
}
.background(
NavigationLink(
destination: ThreadView(
mainStatus: self.status
),
isActive: self.$goToThread
) {
EmptyView()
}
)
这里是一些复制场景的可能解决方案的演示(因为提供的代码不可测试 as-is)。这个想法是重复使用一个 NavigationLink,但根据激活位置使用不同的目的地。
测试 Xcode 12 / iOS 14
struct DemoRowView: View {
@State private var isProfile = false
@State private var isActive = false
var body: some View {
HStack {
Image(systemName: "person")
.scaledToFit()
.onTapGesture {
self.isProfile = true
self.isActive = true
}
Text("Thread description")
.onTapGesture {
self.isProfile = false
self.isActive = true
}
.background(
NavigationLink(destination: self.destination(), isActive: $isActive) { EmptyView() }
)
}
.buttonStyle(PlainButtonStyle())
}
@ViewBuilder
private func destination() -> some View {
if isProfile {
ProfileView()
} else {
ThreadView()
}
}
}