web-dev-qa-db-ja.com

SwiftUIでナビゲーションバーボタンをクリックして新しいビューに移動する方法

.
var body: some View { NavigationView { List(0...< 5) { item in NavigationLink(destination: EventDetails()){ EventView() } } .navigationBarTitle("Events") .navigationBarItems(trailing: NavigationLink(destination: CreateEvent()){ Text("Create Event") } ) } }

5
Unikorn

これは私にとってはうまくいきます:

    .navigationBarItems(trailing: HStack { AddButton(destination: EntityAddView()) ; EditButton() } )

どこ:

struct AddButton<Destination : View>: View {

    var destination:  Destination

    var body: some View {
        NavigationLink(destination: self.destination) { Image(systemName: "plus") }
    }
}
1
Chuck H

現在のところiOS13のバグです https://forums.developer.Apple.com/thread/124757

「sort-of」回避策はここにあります: https://stackoverflow.com/a/57837007/4514671

これが私の解決策です:MasterView-

import SwiftUI

struct MasterView: View {

    @State private var navigationSelectionTag: Int? = 0

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DestinationView(), tag: 1, selection: self.$navigationSelectionTag) {
                    EmptyView()
                }
                Spacer()
            }
            .navigationBarTitle("Master")
            .navigationBarItems(trailing: Button(action: {
                self.navigationSelectionTag = 1
            }, label: {
                Image(systemName: "person.fill")
            }))
        }
    }
}

struct MasterView_Previews: PreviewProvider {
    static var previews: some View {
        MasterView()
    }
}

そしてDetailsView-

import SwiftUI

struct DetailsView: View {
    var body: some View {
        Text("Hello, Details!")
    }
}

struct DetailsView_Previews: PreviewProvider {
    static var previews: some View {
        DetailsView()
    }
}
0
Rebeloper