按搜索键时如何关闭键盘?我在tableView上有一个搜索栏,我使用了以下方法.它没用.任何想法为什么?提前致谢.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Run Code Online (Sandbox Code Playgroud)
更新2.0 ------------------------------------------------ ----
这张照片是我想要做的,当我点击搜索键时什么也没发生. 在此输入图像描述
我已经设置了我的UISearchBar委托
class SearchVC: UIViewController, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var data = [// my data here.]
var filteredData: [String]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
filteredData = data
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tableView.addGestureRecognizer(tap)
}
Run Code Online (Sandbox Code Playgroud) 我有用户的当前位置和特定位置。我试图以英里为单位获得距离。我的英里距离似乎不对,我在我的应用程序上得到了 1.2 英里,然后我在谷歌地图上查看了英里数。我在谷歌地图上有 2.1 英里。这是我的代码,我不确定为什么我的代码不准确。请帮忙。
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
map.userTrackingMode = MKUserTrackingMode.follow
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
// set location
let latitude: CLLocationDegrees = 32.5850
let longitude: CLLocationDegrees = -85.4904
let latDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinates, span: span)
map.setRegion(region, animated: true)
// add annotation
let …Run Code Online (Sandbox Code Playgroud)