简体   繁体   中英

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

I have two buttonStyles, mainButtonOn and mainButtonOff. I want my button to use one or the other based on its toggle state. The following code throws the error "Unable to infer complex closure return type; add explicitly type to disambiguate."

I'm brand new to SwiftUI, so if you can see the problem I'd love it if you'd dumb your answer way down. Thanks, everyone.

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
        }
    }
}

one approach would be to bypass the problem:

            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())
        }

another more concise approach is this:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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