1

I have added a "Show nearby friends" button for the search screen but isn't working. If I search for "something" and then push the "Show nearby friends" the search query gets removed and nothing happens.

Here it's the code:

// MARK: - QUERY USERS
func queryUsers(text: String, location: Bool) {
    showHUD(message: "Searching...")
    let keywords = text.lowercased().components(separatedBy: " ")

    let query = PFUser.query()!
    //if location == false {
        query.whereKey(USER_FULLNAME_LOWERCASE, containedIn: keywords)
    //}
    // Search nearby 100 Km
    if location == true {
        if currLocation != nil {
            let gp = PFGeoPoint(latitude: currLocation!.coordinate.latitude, longitude: currLocation!.coordinate.longitude)
            query.whereKey(USER_LOCATION, nearGeoPoint: gp, withinKilometers: 100)
        }
    }

    query.findObjectsInBackground(block: { (users, error) in
        if error == nil {
            self.foundFriendsArray = users!
            self.searchTableView.reloadData()
            self.hideHUD()

        } else {
            self.simpleAlert(mess: "\(error!.localizedDescription)")
            self.hideHUD()
        }})
}
// MARK: - SERACH BAR DELEGATES
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    if searchBar.text == "" {
        simpleAlert(mess: "You must type a name to search for friends")
    } else {
        queryUsers(text: searchBar.text!, location: true)
        searchBar.showsCancelButton = false
        searchBar.resignFirstResponder()
    }
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.showsCancelButton = true
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.showsCancelButton = false
    searchBar.resignFirstResponder()
}





// MARK: - SEARCH NEARBY FRIENDS BUTTON
@IBAction func searchNearbyButt(_ sender: Any) {
    searchBar.text = ""
    searchBar.showsCancelButton = false
    searchBar.resignFirstResponder()

    // Call query
    queryUsers(text: searchBar.text!, location: true)
}

What am I doing wrong?

agis
  • 1,831
  • 10
  • 33
  • 68
  • Can you explain more? The search query gets removed because you call `searchBar.text = ""`, am I correct? – chengsam Apr 10 '17 at 15:39
  • When I type "something" and hit search it works just fine. If I tap on "Show nearby..." the query gets removed and no result it's showed. How do I keep the search query and show just the nearby users who match that specific keyword used in search. – agis Apr 10 '17 at 16:02

0 Answers0