gpt4 book ai didi

swift - 尝试以编程方式制作退格按钮以执行与键盘删除 (ᗏX) 按钮相同的操作

转载 作者:行者123 更新时间:2023-11-28 07:26:22 25 4
gpt4 key购买 nike

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

还尝试了两次退格

@objc func searchButtonTap() { 
let name: String = searchBar.text!
let endIndex = name.index(name.endIndex, offsetBy: -2)
let truncated = String(name[..<endIndex])
searchBar.text = truncated
}

最佳答案

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

@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)
}

关于swift - 尝试以编程方式制作退格按钮以执行与键盘删除 (ᗏX) 按钮相同的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56504746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com