skip to Main Content

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


  1. It’s quite easy to handle NavigationLink in this case. Notice you have to wrap the body into a NavigationStack to make it work. And a NavigationLink is tappable as well, so you don’t need a Button.

    NavigationStack {
        Section("Windows") {
            NavigationLink {
                // NewOrderView here
            } label: {
                Text("New order")
                // Or `SettingRowView`
            }
        }
    }
    
    Login or Signup to reply.
  2. You can use the NavigationStack in conjunction with NavigationLink(value:,label:) and navigationDestination, which is the "newer" way of handling navigation in SwiftUI.

    NavigationStack {
        Section("Windows") {
            NavigationLink(value: "this-string-defines-the-destination") {
                SettingRowView(...)
            }
        }
        .navigationDestination(for: String.self) { navigationValue in
            SettingDetailView(for: navigationValue)
        }
    }
    

    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 with String 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search