单击按钮时如何显示视图?

时间:2019-07-02 13:39:48

标签: swift swiftui

我正在尝试使用Apple的 SwiftUI 制作应用,并且我需要有两个按钮,它们可以在单个List行中显示两个不同的视图。

我使用 Xcode beta 2 MacOS Catalina beta 2 。我尝试添加显示视图的Button,但是我无法单击它,而当我尝试在Button之外的简单List并单击它时,{{ 1}}视图未出现。我也尝试过在AddList()内添加navigationButton,但是它也不起作用。单击navigationButton也不起作用,该视图仍然不会出现

tapAction

我希望NavigationView { List(0..<5) { item in NavigationButton(destination: ContentOfList(), isDetail: true) { Text("hello") // dummy text Spacer() Text("edit") .tapAction { AddList() // This is the view I want to present } } }.navigationBarItems(trailing: NavigationButton(destination: AddList(), label: { // doesn't work within navigationBarItems Image(systemName: "plus.circle.fill") })) } 视图会出现,但在两种情况下却不会。

3 个答案:

答案 0 :(得分:2)

更新:NavigationButton寿命很短。在beta3中,它已被弃用。我正在更新代码以使用其替换:NavigationLink

您可以从所有三个位置显示视图。方法如下:

enter image description here

import SwiftUI

struct ContentView: View {

    var body: some View {
        NavigationView {
            TopView().navigationBarTitle(Text("Top View"))
        }
    }
}

struct TopView: View {

    let detailView = DynamicNavigationDestinationLink(id: \String.self) { data in
        AddList(passedData: data)
    }

    var body: some View {
            List(0..<5) { item in
                NavigationLink(destination: AddList(passedData: "FROM ROW #\(item)")) {
                    HStack {
                        Text("Row #\(item)")
                        Spacer()
                        Text("edit")
                            .tapAction {
                                self.detailView.presentedData?.value = "FROM TAP ACTION Row #\(item)"
                        }
                    }
                }
            }.navigationBarItems(trailing: Button(action: {
                            self.detailView.presentedData?.value = "FROM PLUS CIRCLE"
            }, label: {
                    Image(systemName: "plus.circle.fill")
                }))
    }
}

struct AddList: View {
    let passedData: String

    var body: some View {
        Text(passedData)
    }
}

答案 1 :(得分:2)

改进版本(SwiftUI,iOS 13 beta 7)

相同的解决方案适用于消除.sheet修饰符提供的模态。

import SwiftUI

struct DetailView: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        Button(
            "Here is Detail View. Tap to go back.",
            action: { self.presentationMode.wrappedValue.dismiss() }
        )
    }
}

struct RootView: View {
    var body: some View {
        VStack {
            NavigationLink(destination: DetailView())
            { Text("I am Root. Tap for Detail View.") }
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            RootView()
        }
    }
}

答案 2 :(得分:0)

改进版本。 (迅速,iOS 13 Beta 4)

class NavigationModel : BindableObject {
    var willChange = PassthroughSubject<Void, Never>()
    var presentedData: String? {
        didSet {
            willChange.send()
        }
    }
    func dismiss() { if presentedData != nil {
        presentedData = nil
    } }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            MasterView()
        }.environmentObject(NavigationModel())
    }
}

struct MasterView: View {

    @EnvironmentObject
    var navigationModel: NavigationModel
    var destinationLink = DynamicNavigationDestinationLink<String, String, DetailView>(id: \.self) { data in DetailView(data: data) }

    var body: some View {
        List(0..<10) { index in
            Button("I am root. Tap for more details of #\(index).") {
                self.navigationModel.presentedData = "#\(index)"
            }
        }
        .navigationBarTitle("Master")
        .onReceive(navigationModel.willChange) {
            self.destinationLink.presentedData?.value = self.navigationModel.presentedData
        }
    }
}

struct DetailView: View {
    @EnvironmentObject
    var model: NavigationModel

    let data: String

    var body: some View {
        Button("Here are details of \(data). Tap to go back.") {
            self.model.dismiss()
        }
        .navigationBarTitle("Detail \(data)")
    }
}

struct Empty : Hashable {
}


#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif