How to navigate in SwiftUI while also executing logic and creating a new Object? [closed]

I have a EditTapasView which is there to edit any Tapas created by the user. To create one, they can press a “New Tapas” button, sending them to exactly this EditTapasView with a newly created Tapas.

Until now it all worked with using a sheet.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Button(action: {
newTapas = addTapas()
/// Delay the toggle of the Sheet by .1 second.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
shouldPresentSheet.toggle()
}
}, label: {
Text("New Tapas")
.font(.system(size: 17))
.padding(.trailing)
.padding([.top, .bottom], 10)
})
.sheet(isPresented: $shouldPresentSheet) {
print("Sheet dismissed!")
} content: {
EditTapasView(tapas: newTapas)
}
</code>
<code>Button(action: { newTapas = addTapas() /// Delay the toggle of the Sheet by .1 second. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { shouldPresentSheet.toggle() } }, label: { Text("New Tapas") .font(.system(size: 17)) .padding(.trailing) .padding([.top, .bottom], 10) }) .sheet(isPresented: $shouldPresentSheet) { print("Sheet dismissed!") } content: { EditTapasView(tapas: newTapas) } </code>
Button(action: {
    newTapas = addTapas()
    /// Delay the toggle of the Sheet by .1 second.
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
         shouldPresentSheet.toggle()
    }
}, label: {
    Text("New Tapas")
        .font(.system(size: 17))
        .padding(.trailing)
        .padding([.top, .bottom], 10)
})
   .sheet(isPresented: $shouldPresentSheet) {
       print("Sheet dismissed!")
    } content: {
       EditTapasView(tapas: newTapas)
    }

But now I have added a new NavigationLink inside of the EditTapasView to make some edits, and this part is not working in a sheet.

When I just use a NavigationLink and giving it the addTapas() function, it constantly creates new Tapas without any click.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NavigationLink(destination: EditTapasView(tapas: addTapas())) {
Text("New Tapas")
.font(.system(size: 17))
.padding(.trailing)
.padding([.top, .bottom], 10)
}
</code>
<code>NavigationLink(destination: EditTapasView(tapas: addTapas())) { Text("New Tapas") .font(.system(size: 17)) .padding(.trailing) .padding([.top, .bottom], 10) } </code>
NavigationLink(destination: EditTapasView(tapas: addTapas())) {
    Text("New Tapas")
        .font(.system(size: 17))
        .padding(.trailing)
        .padding([.top, .bottom], 10)
}

Then I researched how to navigate progamatically, but I couldn’t make it work.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NavigationStack (path: $path) {
Button(action: {
newTapas = addTapas()
path = EditTapasView(tapas: newTapas)
}, label: {
Text("New Tapas")
.font(.system(size: 17))
.padding(.trailing)
.padding([.top, .bottom], 10)
})
}
</code>
<code>NavigationStack (path: $path) { Button(action: { newTapas = addTapas() path = EditTapasView(tapas: newTapas) }, label: { Text("New Tapas") .font(.system(size: 17)) .padding(.trailing) .padding([.top, .bottom], 10) }) } </code>
NavigationStack (path: $path) {
    Button(action: {
        newTapas = addTapas()
        path = EditTapasView(tapas: newTapas)
    }, label: {
        Text("New Tapas")
            .font(.system(size: 17))
            .padding(.trailing)
            .padding([.top, .bottom], 10)
    })
}

So is there any way to solve my problem? Either to simply use NavigationLink (or similar) in the Action part of a button? Or to have some logic execute once a NavigationLink is pressed?

3

There’s probably a few different ways a simple way would be to use navigationDestination if it’s not too buggy. The NavigationStacks and NavigationSplitViews aren’t really ready for production yet.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@State private var newTapa: Tapa?
var body: some View {
NavigationStack {
Button("Add Tapas") {
newTapa = Tapa()
}
.navigationDestination(item: newTapa) { tapa in
TapaEditView(tapa: tapa)
}
}
}
</code>
<code>@State private var newTapa: Tapa? var body: some View { NavigationStack { Button("Add Tapas") { newTapa = Tapa() } .navigationDestination(item: newTapa) { tapa in TapaEditView(tapa: tapa) } } } </code>
@State private var newTapa: Tapa?

var body: some View {
        NavigationStack {
            Button("Add Tapas") {
                newTapa = Tapa()
            }
            .navigationDestination(item: newTapa) { tapa in
                TapaEditView(tapa: tapa)
            }
        }
}

//UPDATE:

The following code compiles just fine. In the code above I forgot to make newTapa a binding. Other than that you may have a compile issue elsewhere.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import SwiftUI
struct Tapa: Hashable {
var name = "Tapa Name"
}
struct TapaEditView: View {
let tapa: Tapa
var body: some View {
Text(tapa.name)
}
}
struct SomeView: View {
@State private var newTapa: Tapa?
var body: some View {
NavigationStack {
Button("Add Tapas") {
newTapa = Tapa()
}
.navigationDestination(item: $newTapa) { tapa in
TapaEditView(tapa: tapa)
}
}
}
}
</code>
<code>import SwiftUI struct Tapa: Hashable { var name = "Tapa Name" } struct TapaEditView: View { let tapa: Tapa var body: some View { Text(tapa.name) } } struct SomeView: View { @State private var newTapa: Tapa? var body: some View { NavigationStack { Button("Add Tapas") { newTapa = Tapa() } .navigationDestination(item: $newTapa) { tapa in TapaEditView(tapa: tapa) } } } } </code>
import SwiftUI


struct Tapa: Hashable {
    var name = "Tapa Name"
}

struct TapaEditView: View {
    let tapa: Tapa

    var body: some View {
        Text(tapa.name)
    }
}

struct SomeView: View {
    @State private var newTapa: Tapa?

    var body: some View {
        NavigationStack {
            Button("Add Tapas") {
                newTapa = Tapa()
            }
            .navigationDestination(item: $newTapa) { tapa in
                TapaEditView(tapa: tapa)
            }
        }
    }
}

1

NavigationPath is correct for programatic navigation, just need to change it to this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NavigationStack (path: $path) {
Button(action: {
let newTapas = newTapas()
path.append(newTapas) // this calls navigationDestination
}, label: {
Text("New Tapas")
.font(.system(size: 17))
.padding(.trailing)
.padding([.top, .bottom], 10)
})
.navigationDestination(for: Tapas.self) { newTapas in
EditTapasView(tapas: newTapas)
}
}
</code>
<code>NavigationStack (path: $path) { Button(action: { let newTapas = newTapas() path.append(newTapas) // this calls navigationDestination }, label: { Text("New Tapas") .font(.system(size: 17)) .padding(.trailing) .padding([.top, .bottom], 10) }) .navigationDestination(for: Tapas.self) { newTapas in EditTapasView(tapas: newTapas) } } </code>
NavigationStack (path: $path) {
    Button(action: {
        let newTapas = newTapas()
        path.append(newTapas)  // this calls navigationDestination
    }, label: {
        Text("New Tapas")
            .font(.system(size: 17))
            .padding(.trailing)
            .padding([.top, .bottom], 10)
    })
    .navigationDestination(for: Tapas.self) { newTapas in
        EditTapasView(tapas: newTapas)
    }
}

And your Tapas model type needs to be a class in this case.

Note in your first attempt that uses DispatchQueue, you can’t use asyncAfter in SwiftUI because the Views are just structs, i.e. values that have no lifetime. After the UI has been described the Views are gone.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật