代码之家  ›  专栏  ›  技术社区  ›  Benjamin RD

使用代理调用另一个视图

  •  0
  • Benjamin RD  · 技术社区  · 4 年前

    我有自己的按钮控制,非常类似于雨燕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()
    //        }
        }
    

    如何从委托操作重定向到另一个视图?有可能吗? 我试了好几种方法,但都没有成功

    1 回复  |  直到 4 年前
        1
  •  0
  •   jnpdx    4 年前

    而不是使用 self.fullScreenCover 在按钮回调中,您需要在视图层次结构中实际使用它。那么,就换个颜色 showLinkTarget fullScreenCover 应提交。

    简化示例:

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

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

    查看有关使用的更多详细信息 全屏封面 https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-full-screen-modal-view-using-fullscreencover