我目前正在将本地搜索集成到我的iOS应用程序中,并且遇到了一些问题。一切都很好,没有任何错误,但是搜索功能并没有发生。我已经在模拟器上和我的iOS设备上运行了它,而且这个函数似乎没有运行。我已经检查了我的控制台日志,没有任何事情发生。
斜体*编辑:我已经修正了方法没有被调用的问题,但是现在我得到了下面的错误。如果需要,我可以粘贴整个调用堆栈。
"searchText:]: unrecognized selector sent to instance 0x7ff2f1c1c3b0 2015-02-16 21:10:26.818 THINKStatus[60477:1795955] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[THINKStatus.loggedinViewController searchText:]: unrecognized selector sent to instance 0x7ff2f1c1c3b0'"
下面是我的视图控制器的代码:
import Foundation
import UIKit
import MapKit
class loggedinViewController : UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var searchText: UITextField!
var matchingItems: [MKMapItem] = [MKMapItem]()
let service = "tgsLogin"
let userAccount = "tgsLoginUser"
let key = "RandomKey"
@IBAction func loggedinActionButton(sender: AnyObject) {
let error = Locksmith.deleteDataForUserAccount("tgsLoginUser", inService: "tgsLogin")
self.performSegueWithIdentifier("logoutViewSegue", sender: self)
}
@IBAction func textFieldReturn(sender: AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
self.performSearch()
}
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,
error: NSError!) in
if error != nil {
println("Error occured in search: \(error.localizedDescription)")
} else if response.mapItems.count == 0 {
println("No matches found")
} else {
println("Matches found")
for item in response.mapItems as [MKMapItem] {
println("Name = \(item.name)")
println("Phone = \(item.phoneNumber)")
self.matchingItems.append(item as MKMapItem)
println("Matching items = \(self.matchingItems.count)")
var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.delegate = self
}
}应用程序代表:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
locationManager = CLLocationManager()
locationManager?.requestWhenInUseAuthorization()
return true
}发布于 2015-02-17 03:54:39
错误消息表明,您可能在某个地方使用searchText,就好像它是一个函数。我在您的代码中没有看到这一点,所以有几种可能性:
我会专注于前两件事。试着检查所有错误消息,看看代码中是否有任何失败的行指示。试着清理项目并进行重建。尝试在所有源文件中搜索"searchText“.
发布于 2016-01-27 00:02:57
这是对我有用的密码。我看到的唯一不同是一些问号,而不是感叹号。如果这对你有用的话请告诉我。
@IBAction func textFieldReturn(sender: AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
self.performSearch()
}
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({
(response: MKLocalSearchResponse?,error: NSError?) in
if error != nil {
print("Error occured in search: \(error!.localizedDescription)")
} else if response!.mapItems.count == 0 {
print("No matches found")
} else {
print("Matches found")
for item in response!.mapItems {
print("Name = \(item.name)")
print("Phone = \(item.placemark)")
self.matchingItems.append(item as MKMapItem)
print("Matching items = \(self.matchingItems.count)")
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = address
self.mapAnnotations.append(annotation)
self.mapView.addAnnotation(annotation)
}
}
})
}https://stackoverflow.com/questions/28553466
复制相似问题