I have a strange SwiftUI problem. In the code below, I display a button in the app that the user uses to enter an API key necessary for app operation. The button displays an alert with a textfield and Cancel and OK buttons. The OK button is disabled until there is some text entered in the textfield. After the enter their API key, it goes on to validate and install the key.
Everything works fine in the MacOS version, but on the IOS version the OK button doesn't show up. Only the Cancel button is displayed. If I set .disabled to false, the OK button shows up. If I set .disabled to true, the button vanishes. I don't want to to vanish!
I'm using Swift 5.7.2 and Xcode 14.2 and SwiftUI. I just can't figure this one out and any ideas would be most welcome.
struct keyView: View {
@AppStorage("API_KEY") var myKey: String?
@State private var showingDialog: Bool = false
@State private var apiKey = ""
@State private var showStatus: Bool = false
@State private var statusMessage: String = ""
var body: some View {
Button {
showingDialog.toggle()
} label: {
Image(systemName: "key.horizontal")
.font(.system(size: 16))
.help("Enter API Key")
.padding(EdgeInsets( top: 1, leading: 0, bottom: 0, trailing: 2))
.foregroundColor(myKey == "" ? .red : .secondary)
}
.buttonStyle(.borderless)
.alert("API Key is Required", isPresented: self.$showingDialog) {
TextField("Enter API Key", text: $apiKey)
.frame(height: 50)
Button("Cancel", role: .cancel) { }
Button("OK", action: setUserKey)
.disabled(self.apiKey.isEmpty) // <- <- <- <- Problem Here
} message: {
Text("Please enter your API key")
.lineLimit(nil)
}
.sheet(isPresented: $showStatus) {
StatusDialog(message: self.statusMessage, isPresented: self.$showStatus)
}
}
}