我有自己的按钮控制,非常类似于雨燕5。
我的协议:
public protocol RoundedButtonDelegate {
func click(button:RoundedButton)
}
我的控制:
public struct RoundedButton : View {
public var text:String = ""
public var backgroundColor:String = ""
public var borderColor:String = ""
public var textColor:String = ""
public var width:CGFloat = 0
public var height:CGFloat = 0
public var fontSize:CGFloat = 0
public var delegate:RoundedButtonDelegate?
public var controlId:String = ""
init(id:String = "", text:String, width:CGFloat, height:CGFloat, fontSize:CGFloat = 19, theme: Theme = Theme.light, delegate:RoundedButtonDelegate?) {
if theme == Theme.light {
self.backgroundColor = LightColorSchema.mainColor.rawValue
self.borderColor = LightColorSchema.mainText.rawValue
self.textColor = LightColorSchema.mainText.rawValue
}
if theme == Theme.dark {
self.backgroundColor = DarkColorSchema.mainColor.rawValue
self.borderColor = DarkColorSchema.mainText.rawValue
self.textColor = DarkColorSchema.mainText.rawValue
}
self.controlId = id
self.text = text
self.width = width
self.height = height
self.fontSize = fontSize
self.delegate = delegate
}
public var body: some View {
ZStack {
Button(action: {
print(">Redirect to next page")
self.delegate?.click(button: self)
}) {
HStack(alignment: .center, spacing: 5.0) {
Text(self.text)
.font(.custom("Montserrat-SemiBold", size: self.fontSize))
.foregroundColor(Color.init(hex: self.textColor))
.padding(.all, 10.0)
}
}
.padding(.all, 10)
.frame(width: self.width, height: self.height, alignment: .center)
.overlay(
RoundedRectangle(cornerRadius: self.height / 2)
.stroke(Color.init(hex: self.borderColor), lineWidth: 1)
).background(RoundedRectangle(cornerRadius: 40).fill(Color.init(hex: self.backgroundColor)))
}
.frame(width: self.width, height: self.height, alignment: .center)
}
}
这不是一个特殊的控件,只是定义一些UI组件。嗯,在我看来,我的实现方式是:
func click(button: RoundedButton) {
print("Execute protocol")
switch button.controlId {
case "btnSesion":
return
case "btnTarjeta":
return self.redirectSeleccionarTarjeta()
default:
print("No defined action")
}
}
func redirectSeleccionarTarjeta() {
print(">Redirect")
self.showLinkTarget = true
self.fullScreenCover(isPresented: $showLinkTarget, content: {
InicioSolicitarTarjetaView()
})
}
如何从委托操作重定向到另一个视图?有可能吗?
我试了好几种方法,但都没有成功