开发者社区 问答 正文

IOS SWIFT-UIButton在自己的按钮单击事件中未被禁用

我使用基于用户输入的过滤方法搜索位置。一旦在UITextfield中给出InOut,用户就必须为该按钮设置标签。以下是按钮单击事件,我没有看到按钮没有被禁用后,同一按钮被点击。

@IBAction func searchGeoButtonClicked(_ sender: UIButton) {
        var matchedArray = ["No results"]
        var flag = false
         print("Button clicked")
         self.searchGeoLocationBut.isEnabled = false
          dropButton.hide()
        searchGeoTextField.shake()
         if let searchText =  searchGeoTextField.text, !searchText.isEmpty
        {
            matchedArray.removeAll()
            let unichar = "→"
            _ = locdata.filter { (location: Location) -> Bool in
            print("Inside search filter")

                if location.country.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines))
            {
                flag = true
                matchedArray.append(location.country)
            }
                else if location.state.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines))
           {
                flag = true
                matchedArray.append(location.state + unichar + location.country )
            }
            else if location.district.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines))
            {
                flag = true
                matchedArray.append(location.district + unichar + location.state + unichar + location.country )
             }
            else if location.subdistrict.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines))
             {
                flag = true
                matchedArray.append(location.subdistrict + unichar + location.district + unichar + location.state + unichar + location.country )
            }
            return flag
        // return location.country.contains(searchText) || location.state.contains(searchText) || location.district.contains(searchText) || location.subdistrict.contains(searchText)
                }
            matchedArray = Array(Set(matchedArray)) //Remove duplicates and assign
            dropButton.dataSource = matchedArray
            dropButton.show()
            self.searchGeoLocationBut.isEnabled = true
        }

    }

展开
收起
游客5akardh5cojhg 2019-12-11 21:29:05 530 分享 版权
阿里云 AI 助理回答

从您提供的代码片段来看,这段代码是用Swift编写的,用于在iOS应用中根据用户在UITextField中输入的文本搜索位置信息。点击按钮后,程序会过滤一个名为locdata的位置数据集合,并根据输入的文本更新一个下拉列表(可能通过自定义实现或第三方库)。如果找到匹配项,按钮应该在处理完请求后重新启用。

问题在于您提到按钮没有在预期的情况下被重新启用。根据您的代码,我注意到以下几点可能影响按钮状态恢复:

  1. 逻辑结构:确保所有的执行路径都考虑到了按钮状态的重置。在当前代码中,只有当searchGeoTextField.text不为空且经过过滤逻辑之后,按钮才会被重新启用。请检查是否有其他条件分支或异常情况导致这部分逻辑未被执行。

  2. 异步操作:如果locdata.filter内部或者与之相关的操作涉及到了异步处理(例如网络请求),那么当前的同步代码结构可能无法正确反映异步完成时的状态变化。在这种情况下,需要在异步操作完成的回调中重新启用按钮。

  3. 错误处理:虽然代码中没有直接显示错误处理逻辑,但在实际应用中,如数据加载失败、解析错误等都可能导致控制流提前退出,从而忘记重置按钮状态。

  4. UI更新问题:有时候,即使在代码中修改了UI控件的状态,由于主线程的更新队列问题,这些改变可能没有立即反映出来。可以尝试使用DispatchQueue.main.async来确保UI更新在主线程上执行。

基于以上分析,您可以尝试以下调整以解决按钮状态不更新的问题:

@IBAction func searchGeoButtonClicked(_ sender: UIButton) {
    var matchedArray = ["No results"]
    var flag = false

    print("Button clicked")
    self.searchGeoLocationBut.isEnabled = false
    dropButton.hide()
    searchGeoTextField.shake()

    guard let searchText = searchGeoTextField.text, !searchText.isEmpty else {
        // 如果文本为空,则直接在这里就重新启用按钮并结束方法
        self.searchGeoLocationBut.isEnabled = true
        return
    }

    matchedArray.removeAll()
    let unichar = "→"

    // 假设这里没有异步操作,直接进行过滤
    _ = locdata.filter { location in
        if location.country.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)) ||
           location.state.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)) ||
           location.district.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)) ||
           location.subdistrict.lowercased().contains(searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines))
        {
            flag = true
            let detail = [location.country, location.state, location.district, location.subdistrict].compactMap { $0 }.joined(separator: unichar)
            matchedArray.append(detail)
        }
        return flag
    }

    matchedArray = Array(Set(matchedArray)) // 移除重复项
    dropButton.dataSource = matchedArray
    dropButton.show()

    // 确保在主线程更新UI
    DispatchQueue.main.async {
        self.searchGeoLocationBut.isEnabled = true
    }
}

如果上述调整仍不能解决问题,建议检查是否有其他部分的代码影响了按钮状态,或者是否有异步操作需要特别处理。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答