NavigationView is deprecated, it would be better to use NavigationStack or NavigationSplitView
Post
Replies
Boosts
Views
Activity
Any help on this would be great!
MenuBarExtra vs Menu
MenuBarExtra is like adding a mini app that lives on the menu bar
What you are trying to achieve seems similar to what Menu does (https://developer.apple.com/documentation/swiftui/menu)
Menu items could even have keyboard shortcuts.
Pasted below is from Apple's documentation.
Menu("Actions") {
Button("Duplicate", action: duplicate)
Button("Rename", action: rename)
Button("Delete…", action: delete)
Menu("Copy") {
Button("Copy", action: copy)
Button("Copy Formatted", action: copyFormatted)
Button("Copy Library Path", action: copyPath)
}
}
Use ForEach inside MenuBarExtra as shown below:
@main
struct SwiftBarApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
MenuBarExtra("Title") {
ForEach(appDelegate.menuItems, id:\.self) { menuItemSet in
MenuBarView(menuItemSet: menuItemSet)
}
}
}
}
Problem
NavigationLink and Button both perform actions.
So having both NavigationLink and Button doesn't make sense, so the action of the button is ignored.
Points to note
NavigationLink is used for navigation
NavigationView is deprecated, so use NavigationStack instead
Solution
Given below is sample code using NavigationStack, use onChange(of:) to detect any changes to path.
Tip
You could even pass the path variable as a binding to the destination views
struct ContentView: View {
@State private var path = [Int]()
var body: some View {
NavigationStack(path: $path) {
VStack {
NavigationLink(value: 1) {
Rectangle()
.frame(width: 100, height: 100)
}
NavigationLink(value: 2) {
Rectangle()
.frame(width: 100, height: 100)
}
NavigationLink(value: 3) {
Rectangle()
.frame(width: 100, height: 100)
}
}
.navigationDestination(for: Int.self) { value in
Text("Selected box: \(value)")
}
}
.onChange(of: path) { newPath in
print("path: \(newPath)")
}
}
}
Problem
This happens consistently in the following environment:
macOS: 14.0 Beta (23A5301h)
Xcode: Version 15.0 beta 5 (15A5209g)
Crash happens when adding a new file.
Observation
File gets written to disk then Xcode crashes.
Subsequently after re-opening Xcode able to add a file from disk.
However consisting crashes when creating a new file
Works fine
The same version of Xcode works fine on macOS 13.5
@BabyJ This code works fine in Xcode 5 beta 2. This was only an issue in Xcode 5 beta 1. I suppose you meant Xcode 5 Beta 1
I am not sure if the following helps
Xcode 15 beta 2 release notes
SwiftData
Known Issues
After deleting an item, SwiftUI may attempt to reference the deleted content during the animation causing a crash. (109838173)
Workaround: Explicitly save after a delete.
Start small .... not sure what MoneySplitter in your code is.
This bug has been fixed in Xcode 15.0 beta 2 (15A5161b).
Thanks a lot @malc for pointing out the release notes.
I have also filed a feedback FB12335970 just in case it helps.
@Purkylin_glow @albert.tra
Not sure if this is a good solution, but the following seems to work (it has downsides):
List {
ForEach(students.filter { $0.department?.id == department.id }) { student in
Text(student.name)
}
}
The downside is@Query would monitor changes and refresh the view even if a student is added to a totally different department.
I really wish there is a better solution ...
@Purkylin_glow The problem with using department.students is that when the + button is tapped to add new students, it doesn't show up in the student list unless you go back to the department list and come back to Student list screen.
New changes to department.students are not observed
@albert.tra Thanks, I have added @Relationship to var students in Department but doesn't work.
I have pasted the complete code, you could also check at your end.
I have tried it on Simulator for iPhone 14 Pro
@endecotp Thank you for the good suggestion and @Systems Engineer thanks for the confirmation.
I was so confused if I was missing something obvious.
@Systems Engineer so what is the IPSW file for (and how to use that)? I wish there was documentation about it, all I read was it is used by Apple Configurator but that is only for iOS IPSW files.