eug*_*prg 1 closures swift swiftui
我创建了一个新的子视图,其中包含其他视图的标题。它有一个箭头图标,我想将一个闭包传递给这个子视图(profileClose 是一个函数):
PlainHeader(title: getLocalizedText("edit profile"), action: profileClose)
Run Code Online (Sandbox Code Playgroud)
问题是我希望这个闭包是可选的。所以我决定将它包装起来,() ? = nil但在我每次使用时都这样做之后PlainHeader(...)。Xcode 说"Extra argument 'action' in call"
import SwiftUI
struct PlainHeader: View {
var title: String
let action: (() -> Void)? = nil
var body: some View {
VStack(spacing: 0) {
ZStack {
VStack(spacing: 0) {
HStack {
Image("arrow-back")
.resizable()
.scaledToFit()
.frame(width: 18, height: 7)
.padding(.leading, 31)
Spacer()
}
.onTapGesture {
print("[debugUI] action called")
if self.action != nil {
self.action!()
}
}
}
VStack {
HStack {
Text("\(title)")
.lineLimit(nil)
.multilineTextAlignment(.center)
.frame(maxWidth: 280)
.font(.custom(Font.primaryTextFont, size: 20))
.foregroundColor(Color.mainSubtitleColor)
}
}
}
.padding(.top, 20)
}
.frame(height: 80)
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我只是想在有闭包和没有闭包的情况下使用它..所以我将它设置为可选并给它一个默认值 nil ..
| 归档时间: |
|
| 查看次数: |
3391 次 |
| 最近记录: |