###プレビューで書く変数が分からなく困っています
SwiftUIを勉強し始めて1週間の者です。
練習としてロック画面を作っていますが
プレビュー画面にエラーが出てここから進みません。
エラー内容としては
[Missing argument for parameter 'handler' in call
Insert 'handler: <#(String, (Bool) -> Void) -> Void#>']
です。
どの様に書けばクリアできるのでしょうか?
教えてください。お願いします。
import SwiftUI // import Introspect public struct Lock: View { var maxDigits: Int = 4 var label = "Enter One Time Password" @State var pin: String = "" @State var showPin = false @State var isDisabled = false var handler: (String, (Bool) -> Void) -> Void public var body: some View { VStack(spacing: 30) { Text(label).font(.title) ZStack { pinDots backgroundField } showPinStack } } private var pinDots: some View { HStack { Spacer() ForEach(0..<maxDigits) { index in Image(systemName: self.getImageName(at: index)) .font(.system(size:30, weight:.ultraLight, design: .default)) Spacer() } } } private var backgroundField: some View { let boundPin = Binding<String>(get: { self.pin }, set: { newValue in self.pin = newValue self.submitPin() }) return TextField("", text: boundPin, onCommit: submitPin) // Introspect library can used to make the textField become first resonder on appearing // if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61 .accentColor(.clear) .foregroundColor(.clear) .keyboardType(.numberPad) .disabled(isDisabled) // .introspectTextField { textField in // textField.tintColor = .clear // textField.textColor = .clear // textField.keyboardType = .numberPad // textField.becomeFirstResponder() // textField.isEnabled = !self.isDisabled // } } private var showPinStack: some View { HStack { Spacer() if !pin.isEmpty { showPinButton } } .frame(height:30) .padding([.trailing]) } private var showPinButton: some View { Button(action: { self.showPin.toggle() }, label: { self.showPin ? Image(systemName: "eye.slash.fill").foregroundColor(.primary) : Image(systemName: "eye.fill").foregroundColor(.primary) }) } private func submitPin() { guard !pin.isEmpty else { showPin = false return } if pin.count == maxDigits { isDisabled = true handler(pin) { isSuccess in if isSuccess { print("pin matched, go to next page, no action to perfrom here") } else { pin = "" isDisabled = false print("this has to called after showing toast why is the failure") } } } // this code is never reached under normal circumstances. If the user pastes a text with count higher than the // max digits, we remove the additional characters and make a recursive call. if pin.count > maxDigits { pin = String(pin.prefix(maxDigits)) submitPin() } } private func getImageName(at index: Int) -> String { if index >= self.pin.count { return "circle" } if self.showPin { return self.pin.digits[index].numberString + ".circle" } return "circle.fill" } } extension String { var digits: [Int] { var result = [Int]() for char in self { if let number = Int(String(char)) { result.append(number) } } return result } } extension Int { var numberString: String { guard self < 10 else { return "0" } return String(self) } } struct Lock_Previews: PreviewProvider { static var previews: some View { Lock() } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/18 15:41
2020/04/19 01:54
2020/04/20 16:09