【发布时间】:2020-12-01 22:07:56
【问题描述】:
我有一个文本字段,当用户在其中键入时,它会触发一个弹出框,显示一个用户可以从中选择的建议值列表。这是文本字段:
lazy var iata: CustomTextField = {
let field = CustomTextField(
title: Strings.originAiportCode.localized,
placeholder: Strings.originAirportCodePlaceholder.localized,
allowSpaceInput: false)
field.configureField(with: TextInputKeyboardSettings(
capitalization: .allCharacters,
spellCheck: .no,
returnKey: .done
))
field.textDidChangeAction = { [weak self] in
self?.search(field: field)
self?.iataError.isHidden = true
}
field.shouldEndEditingAction = { [weak self] in
self?.verifyTextfieldInput()
}
validator.registerField(field, rules: [RequiredRule(message: Strings.eHandshake.validation.iataRequired.localized)])
return field
}()
didChangeAction 在 CustomTextField 类上声明如下:
public var textDidChangeAction: (() -> Void)?
这作为一个闭包传递给委托方法,如下所示:
public func textFieldDidChangeSelection(_ textField: UITextField) {
textDidChangeAction?()
}
这里是搜索方法:
func search(field: CustomTextField) {
if iata.text.isEmpty {
popover.dismiss(animated: true, completion: nil)
} else {
let filterCompletion: (Int) -> () = { count in
self.popover.sourceView = field
// Present if needed
let isPopoverPresented = self.popover.isVisiblyPresented
if !isPopoverPresented && count > 0 {
self.present(self.popover, animated: false, completion: nil)
}
if isPopoverPresented && count == 0 {
self.popover.dismiss(animated: false, completion: nil)
}
}
popover.filterToSearchTerm(field.text, objects: airportsList, completion:filterCompletion)
}
}
现在一切正常,如果用户在文本字段中输入得太快,在这种情况下我会遇到崩溃并出现以下错误:
Thread 1: "Application tried to present modally a view controller <project.ContainerPopover: 0x7fc736eabeb0> that is already being presented by <project.TabBarController: 0x7fc737838200>."
这个弹出框正在接收的数据列表非常大,很明显,应用程序正在挣扎,因为它试图在 vc 已经存在时呈现它。但是,我对如何解决这个问题感到困惑。任何帮助将不胜感激。
【问题讨论】:
标签: swift delegates popover uitextfielddelegate