将视图作为参数传递给按钮,将其作为模态触发
Pass view as parameter to Button triggering it as a Modal
我想要一个自定义按钮结构,它接收一个视图作为参数,单击按钮时将显示为模态。但是,视图参数始终为空,我找不到我在做的错误。我的按钮结构如下所示:
struct InfoButton<Content:View>: View {
@State private var showingInfoPage: Bool
private var infoPage: Content
init(infoPage: Content, showingInfoPage: Bool) {
self.infoPage = infoPage
_showingInfoPage = State(initialValue: showingInfoPage)
}
var body: some View {
return
Button(action: {
self.showingInfoPage.toggle()
}) {
Image(systemName: "info.circle")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(.white)
.padding()
}.sheet(isPresented: self.$showingInfoPage) {
self.infoPage
}.frame(minWidth: 0, maxWidth: .infinity, alignment: .topTrailing)
}
}
此按钮位于我为多个其他视图创建的模板的导航栏中。
我认为该模板最相关的部分是:
protocol TrainingView {
var title: String { get }
var subheadline: String { get }
var asAnyView: AnyView { get }
var hasInfoPage: Bool { get }
var infoPage: AnyView { get }
}
extension TrainingView where Self: View {
var asAnyView: AnyView {
AnyView(self)
}
var hasInfoPage: Bool {
false
}
var infoPage: AnyView {
AnyView(EmptyView())
}
}
struct TrainingViewTemplate: View {
@State var showInfoPage: Bool = false
@State var viewIndex: Int = 0
var body: some View {
//the views that conform to the template
let views: [TrainingView] = [
ExerciseView(),
TrainingSessionSummaryView()
]
return NavigationView {
ViewIterator(views, self.$viewIndex) { exerciseView in
VStack {
VStack {
Text(exerciseView.title)
.font(.title)
.fontWeight(.semibold).zIndex(1)
Text(exerciseView.subheadline)
.font(.subheadline)
Spacer()
exerciseView.asAnyView.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}.navigationBarItems(trailing: (exerciseView.hasInfoPage == true ? InfoButton(infoPage: exerciseView.infoPage, showingInfoPage: self.showInfoPage) : nil))
}
}
}
我调试到初始化 navigationBarItems 的地步。此时,练习视图包含“hasInfoPage”和“infoPage”本身的内容。
一个示例性练习视图有一个 header 像这样:
struct ExerciseView: View, TrainingView {
var title: String = "Strength Session"
var subheadline: String = "Pushups"
var numberOfExercise: Int = 1
@State var ratingValue: Double = 0
@Environment(\.presentationMode) var presentationMode
var hasInfoPage: Bool = true
var infoPage = ExerciseDetailView()
因此,在此视图中,信息页面使用我在模板视图中收到的 ExercieDetailView() 进行初始化,但是只要单击信息按钮,调试器就会显示一个空信息页面,即使“showingInfoPage”变量包含正确的值。
您未确认协议,因此显示 extension TrainingView
中的默认 infoPage
。
解决方法是
struct ExerciseView: View, TrainingView {
// .. other code here
var infoPage = AnyView(ExerciseDetailView()) // << here !!
``
我想要一个自定义按钮结构,它接收一个视图作为参数,单击按钮时将显示为模态。但是,视图参数始终为空,我找不到我在做的错误。我的按钮结构如下所示:
struct InfoButton<Content:View>: View {
@State private var showingInfoPage: Bool
private var infoPage: Content
init(infoPage: Content, showingInfoPage: Bool) {
self.infoPage = infoPage
_showingInfoPage = State(initialValue: showingInfoPage)
}
var body: some View {
return
Button(action: {
self.showingInfoPage.toggle()
}) {
Image(systemName: "info.circle")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(.white)
.padding()
}.sheet(isPresented: self.$showingInfoPage) {
self.infoPage
}.frame(minWidth: 0, maxWidth: .infinity, alignment: .topTrailing)
}
}
此按钮位于我为多个其他视图创建的模板的导航栏中。 我认为该模板最相关的部分是:
protocol TrainingView {
var title: String { get }
var subheadline: String { get }
var asAnyView: AnyView { get }
var hasInfoPage: Bool { get }
var infoPage: AnyView { get }
}
extension TrainingView where Self: View {
var asAnyView: AnyView {
AnyView(self)
}
var hasInfoPage: Bool {
false
}
var infoPage: AnyView {
AnyView(EmptyView())
}
}
struct TrainingViewTemplate: View {
@State var showInfoPage: Bool = false
@State var viewIndex: Int = 0
var body: some View {
//the views that conform to the template
let views: [TrainingView] = [
ExerciseView(),
TrainingSessionSummaryView()
]
return NavigationView {
ViewIterator(views, self.$viewIndex) { exerciseView in
VStack {
VStack {
Text(exerciseView.title)
.font(.title)
.fontWeight(.semibold).zIndex(1)
Text(exerciseView.subheadline)
.font(.subheadline)
Spacer()
exerciseView.asAnyView.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}.navigationBarItems(trailing: (exerciseView.hasInfoPage == true ? InfoButton(infoPage: exerciseView.infoPage, showingInfoPage: self.showInfoPage) : nil))
}
}
}
我调试到初始化 navigationBarItems 的地步。此时,练习视图包含“hasInfoPage”和“infoPage”本身的内容。
一个示例性练习视图有一个 header 像这样:
struct ExerciseView: View, TrainingView {
var title: String = "Strength Session"
var subheadline: String = "Pushups"
var numberOfExercise: Int = 1
@State var ratingValue: Double = 0
@Environment(\.presentationMode) var presentationMode
var hasInfoPage: Bool = true
var infoPage = ExerciseDetailView()
因此,在此视图中,信息页面使用我在模板视图中收到的 ExercieDetailView() 进行初始化,但是只要单击信息按钮,调试器就会显示一个空信息页面,即使“showingInfoPage”变量包含正确的值。
您未确认协议,因此显示 extension TrainingView
中的默认 infoPage
。
解决方法是
struct ExerciseView: View, TrainingView {
// .. other code here
var infoPage = AnyView(ExerciseDetailView()) // << here !!
``