Swift - 呈现已呈现的弹出窗口时应用程序崩溃

Swift - app crash when presenting popover as already presented

我有一个文本字段,当用户在其中输入内容时,它会触发一个弹出窗口,其中会显示一个建议值列表,用户可以从中 select。这是文本字段:

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 class 上声明如下:

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。但是,我对如何解决此问题感到困惑。任何帮助将不胜感激。

我好像已经解决了!我在展示/解散时添加了第二个检查:

(!self.popover.isBeingPresented && !self.popover.isVisiblyPresented)

这似乎可以确保弹出窗口既未显示也未显示,这已解决问题