swift的UISearchBar.value可以做什么?

问题描述

快速使用UITableView和UISearchBar时,我试图找到一种方法,当用户搜索某些内容然后在表格视图中滚动时,使搜索栏上的取消按钮保持启用状态,因此他们不必单击两次取消搜索退出第一响应者(或结束在搜索栏上的编辑)的认行为将使“取消”按钮显示为灰色,但我想使其保持启用状态,因此在Stackoverflow上我找到了如何执行此操作,但找不到答案在线了解searchBar.value(forKey: "cancelButton")在以下代码中的作用。显然,它以某种方式创建了对搜索栏上的“取消”按钮的引用,但我不明白.value的作用(因为我是swift的新手),以及“ cancelButton”键的来源。我已经阅读了func value(forKey key: String)文档,但仍然不明白。如果有人可以解释这是怎么回事。

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        searchBar.resignFirstResponder()
        // If the user scrolls,keep the cancel button enabled
        if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {  // <-- This line
            if searchBar.text != "" { cancelButton.isEnabled = true }
            else { searchBar.showsCancelButton = false }
        }
    }

谢谢。

解决方法

<!DOCTYPE html> <html> <style> </style> <span class="tooltip"> <ul *ngIf="dataLoaded==true" class="list-group"> <li (click)="currentBrand=null" routerLink="/cars" class="list-group-item" [ngClass]="{'active': !currentBrand}"> <span class="tooltiptext"> Listed All Cars </span> All Brands </li> <li class="list-group-item" (click)="currentBrand = brand" routerLink="/cars/brand/{{brand.brandId}}" [ngClass]="{'active': currentBrand === brand}" *ngFor="let brand of brands">{{brand.brandName}}</li> </ul> </span> </body> </html>

的子类

UISearchBar -> UIView -> UIResponder

并且所有 NSObject 都符合 NSKeyValueCoding Protocol Reference

NSObject 是一种 KVC 方法。它适用于任何 valueForKey: 类以及符合上述协议的任何其他类。 NSObject 允许您使用字符串作为名称来访问属性。例如,如果我有一个带有 valueForKey: 属性的 Account 类,我可以执行以下操作:

number

由于是运行时检查,因此无法确定返回类型是什么。所以你必须像这样手动投射它:

let myAccount = Account(number: 12)
myAccount.value(forKey: "number")

我不打算在这里解释向下和可选

因此,您可以访问符合 let number = myAccount.value(forKey: "number") as? Int 的对象的任何属性,只需知道其方法的确切名称(可以通过简单的逆向工程轻松找到)。

还有一个类似的方法叫做 NSKeyValueCoding,可以让你执行对象的任何函数

⚠️ 但请注意,如果您触摸系统的私有变量或函数,Apple 将拒绝您的应用程序。 (如果他们发现了!)

⚠️另外,请注意,任何这些都可以在没有通知的情况下重命名,您的应用将面临未定义的行为。

,

我在搜索列表时遇到了和你一样的问题,但我意识到你可以实现 UITextfields...

使用 didReturn 执行 textfield.resignFirstResponder() 并在它退出时使用 textfield.value 来搜索列表。

,

在 iOS 12 或更低版本的搜索栏中,要访问元素,您可以使用键值来访问元素。喜欢这个功能-

private func configureSearchBar() {
    searchBar.barTintColor = Color.navBarColor
    searchBar.makeRounded(cornerRadius: 5,borderWidth: 1,borderColor: Color.navBarColor)
    searchBar.placeholder = searchBarPlaceholderText
    searchBar.setImage(Images.search_white,for: .search,state: .highlighted)
    searchBar.setImage(Images.green_check,for: .clear,state: .normal)

    if let textField = searchBar.value(forKey: "searchField") as? UITextField {
        textField.textColor = .white
        
        textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder ?? "",attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
        
        let leftSideImage = textField.leftView as? UIImageView
        leftSideImage?.tintColor = .white
    }
    
    if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {
        cancelButton.setTitleColor(.white,for: .normal)
    }
}

在这里,我们通过使用键来访问搜索栏的元素。