尝试以编程方式使退格按钮与键盘删除(ᗏX)按钮相同

时间:2019-06-08 08:24:30

标签: swift

aes_256_cbc

还尝试了两个退格键

@objc func searchButtonTap() { 
    searchBar.becomeFirstResponder()
    var remove = searchBar.text
    remove = String(remove!.dropLast())
    searchBar.text = remove 
}

1 个答案:

答案 0 :(得分:0)

假设您要在用户点击后退(搜索?)按钮时删除搜索栏文本的最后一个字符。 因此,

@objc func searchButtonTap() { 
    guard !(searchBar.text?.isEmpty ?? true) else { return } //Making sure that you don't hit the "Fatal error: Can't remove more items from a collection than it contains"
    searchBar.text?.removeLast(1)
}