I’m new at Xcode and swift UI
I need help how to create a button
Why is it more complicated than Android?
I have view that call Profileview and I want to Navigation to NewOrderview, I know I need to use the NavigationLink.
As you can see at my code I have 2 button as a Section and I like that button that have title "New Order" to Navigation to new View call NewOrderView.
I will be glad for any help
This is my code
Section("Windows"){
Button {
print("")
} label: {
SettingRowView(imageName: "rectangle.portrait.and.arrow.forward.fill",
title: "New Order",
tinColor: .gray)
}
Button{
print("Show Database..")
} label: {
SettingRowView(imageName: "externaldrive.connected.to.line.below.fill",
title: "Show Database",
tinColor: .gray)
}
}
I try to Navigation to a new View , I try to use a NevigationLink
2
Answers
It’s quite easy to handle
NavigationLink
in this case. Notice you have to wrap thebody
into aNavigationStack
to make it work. And aNavigationLink
is tappable as well, so you don’t need aButton
.You can use the
NavigationStack
in conjunction withNavigationLink(value:,label:)
andnavigationDestination
, which is the "newer" way of handling navigation in SwiftUI.This is a bit more complicated than the "old"
NavigationLink(destination: () -> View, label: () -> View)
, but also more flexible.This allows you to "push a value onto the navigation stack". In my example a
String
of value "this-string-defines-the-destination". SwiftUI will then look whether it can find a handler for navigation withString
values and will find the.navigationDestination(for: String.self)
.You can also push other types onto the stack (you can also mix types, e.g. push Strings, Ints or any custom type), as long as they conform to
Hashable
.This documentation page by Apple goes a bit more into detail: https://developer.apple.com/documentation/swiftui/navigationstack
Small note: it’s best to decide on one approach for your entire app.
Either go with the old
NavigationLink(destination: () -> View, label: () -> View)
or go with the new
NavigationLink(value: Hashable, label: () -> View)
.Mixing both approaches will cause issues.