繁体   English   中英

在 SWIFTUI 中不能使用切换行为来触发.buttonStyle

[英]Can't use toggle behaviour to trigger .buttonStyle in SWIFTUI

我有两个按钮样式,mainButtonOn 和 mainButtonOff。 我希望我的按钮根据其开关 state 使用一个或另一个。以下代码抛出错误“无法推断复杂的闭包返回类型;添加显式类型以消除歧义。”

我是 SwiftUI 的新手,所以如果你能看到这个问题,我会很高兴你能把你的答案弄得愚蠢一点。 感谢大家。

import SwiftUI
struct ContentView: View {
    var displayTime = "Ahoy"

    func getTime(oldTime: Int, addedTime: Int) -> Int {
        return oldTime + addedTime
    }

       @State var hoursOn:Bool = true

    struct mainButtonOff: ButtonStyle {

        func makeBody(configuration: Self.Configuration) -> some View {
            configuration.label
                .frame(width: 110, height: 35, alignment: .center)
                .background(Color.white)
                .foregroundColor(Color.gray)
                .cornerRadius(30)
                .overlay(RoundedRectangle(cornerRadius: 30) .stroke(Color.gray, lineWidth: 1))
        }
    }

    struct mainButtonOn: ButtonStyle {

        func makeBody(configuration: Self.Configuration) -> some View {
            configuration.label
                .frame(width: 110, height: 35, alignment: .center)
                .background(Color.blue)
                .foregroundColor(Color.white)
                .cornerRadius(30)
                .overlay(RoundedRectangle(cornerRadius: 30) .stroke(Color.blue, lineWidth: 1))
        }
    }


    var body: some View {

        let displayTime = String(getTime(oldTime: 15, addedTime: 10)) // get time and turn it into a string

        return VStack { // ***** ERROR: Unable to infer complex closure return type; add explicitly type to disambiguate.
            //Spacer()
            Text(displayTime)
            //Spacer()
            Button(action: {self.hoursOn.toggle()}) {
                Text("Hours")
                    .font(.headline)
            }
                .buttonStyle(self.hoursOn ? mainButtonOn() : mainButtonOff())
                //.buttonStyle(mainButtonOff()) // <= USING THIS LINE INSTEAD THROWS NO ERROR
        }
    }
}

一种方法是绕过这个问题:

            if self.hoursOn {
            Button(action: {self.hoursOn.toggle()}) {
                Text("Hours").font(.headline)
            }.buttonStyle(mainButtonOn())
        } else {
            Button(action: {self.hoursOn.toggle()}) {
                Text("Hours").font(.headline)
            }.buttonStyle(mainButtonOff())
        }

另一种更简洁的方法是:

struct mainButtonOnOff: ButtonStyle {
    let onoff: Bool

    init(_ switsh: Bool) {
        self.onoff = switsh
    }

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .frame(width: 110, height: 35, alignment: .center)
            .background(self.onoff ? Color.blue : Color.white)
            .foregroundColor(self.onoff ? Color.white : Color.gray)
            .cornerRadius(30)
            .overlay(RoundedRectangle(cornerRadius: 30) .stroke(self.onoff ? Color.blue : Color.gray, lineWidth: 1))
    }
}

and:

    Button(action: {self.hoursOn.toggle()}) {
            Text("Hours") .font(.headline)
        }.buttonStyle(mainButtonOnOff(self.hoursOn))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM