[英]Complex Custom Navigation SwiftUI
這是我當前用 SwiftUI 編寫的演示 UI:
我想根據是否點擊了單元格(視圖 1、視圖 2、...)或是否點擊了單元格中的按鈕(箭頭)之一來做出自定義導航和顯示決策。
例如,每個單元格代表我想要導航到的不同視圖,每個箭頭按鈕代表我想要呈現的一些自定義模式。
我如何跟蹤上下文並將不同的點擊傳播到最終做出導航決策的視圖。 這是制作列表並做出導航決策的視圖:
import SwiftUI
struct ContentView: View {
@State var options: [String] = ["View 1", "View 2", "View 3", "View 4"]
var body: some View {
ZStack {
Color.white.edgesIgnoringSafeArea(.all)
ScrollView {
ForEach(self.options, id: \.self) { element in
OptionCell(optionDescription: element)
}
}
}
}
}
這是我的OptionCell
class。 我嘗試在此處的按鈕操作中的一些評論中說明我的問題。
struct OptionCell {
//MARK: Input Properties
@State var optionDescription: String
//MARK: Computed Properties
//MARK: Init
//MARK: Functions
func cellTapped() {
///What can I call here to propagate the context of this cell along with the fact that it was tapped to something that needs to make UI transition decisions?
print("\(self.optionDescription) Tapped")
//What can I do to make micro navigation decisions if one of the "ArrowButtons" is tapped?
}
}
extension OptionCell: View {
var body: some View {
Button(action: { self.cellTapped() }) {
VStack {
HStack{
Text(self.optionDescription).foregroundColor(Color.black).padding()
Spacer()
HStack {
//These Views have a Button view in them with a call to a function in that struct. How do I get the call for the tap on each of these buttons propagated up to the decisions making view?
ArrowButton(direction: .LEFT)
ArrowButton(direction: ArrowDirection.RIGHT)
ArrowButton(direction: ArrowDirection.DOWN)
ArrowButton(direction: ArrowDirection.UP)
}.padding()
}
///Another custom view that I have
SeparatorView(separatorViewStyle: SeparatorViewStyle.init(weight: .THIN, gapPosition: .RIGHT, color: Colors.grey.medium))
}
}
}
}
在UIKit
,我將在自定義UITableViewCell
上添加按鈕操作,然后為自定義單元格創建一個委托。 點擊按鈕將調用相應的委托 function (Delegate.leftArrow(), Delegate.upArrow(), ...) 並且UIViewController
將成為自定義單元格的委托並在那里做出導航決定。
我想問題是:
我如何讓點擊每個按鈕的調用傳播到決策制定視圖?
在 ContentView 中,您設置如下:
@ObservedObject var myChoice = MyChoiceClass()
在哪里:
class MyChoiceClass: ObservableObject {
@Published var choice: String = ""
}
將它傳遞給您的 OptionCell(...) 然后傳遞給 ArrowButton(...)
因此,當 ArrowButton(...) 更改“選擇”時,它無處不在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.