Swiftui 使用委托调用另一个视图

Swiftui 使用委托调用另一个视图,swiftui,Swiftui,我有自己的按钮控制,非常类似于swift 5 我的协议: public protocol RoundedButtonDelegate { func click(button:RoundedButton) } 我的控制: public struct RoundedButton : View { public var text:String = "" public var backgroundColor:String = ""

我有自己的按钮控制,非常类似于swift 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)
                }
                //.frame(width: UIScreen.main.bounds.width - 80, height: 48, alignment: .center)
            }
            .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() //Here is accessing!
        default:
            print("No defined action")
        }
    }
这就是我需要解决的方法:

func redirectSeleccionarTarjeta()  {
        print(">Redirect")
        self.showLinkTarget = true
        
        
        //self.navigate(to: InicioSolicitarTarjetaView(), when: $showLinkTarget)
        
            self.fullScreenCover(isPresented: $showLinkTarget, content: {
                InicioSolicitarTarjetaView()
                            })
//        NavigationLink(destination: InicioSolicitarTarjetaView(), isActive: self.$showLinkTarget ) {
//           Spacer().fixedSize()
//        }
    }
如何从委托操作重定向到另一个视图?可能吗?
我尝试了几种方法,但运气不佳,除了在按钮回调中使用
self.fullScreenCover
之外,您还需要在视图层次结构中实际使用它。然后,只需更改回调中的
showLinkTarget
值。下次渲染视图时,应显示
全屏封面

简化示例:

@State var showWindow = false

Button(action: {
    showWindow.toggle()
}) {
    Text("Open window")
}.fullScreenCover(isPresented: $showWindow, content: {
    Text("Content")
})

请注意,这在视图层次结构中,而不是按钮的动作回调


查看有关使用
全屏封面的更多详细信息:

你说得对。我没有注意到
fullScreenCover
可以在很多组件中使用,它帮助我理解了很多,谢谢!