ksa_coder February 2016

Swift - Search always executing before full text

I have a class that handles the user looking for another user via the username. The looking part is done through SearchBar control. Backend is Firebase.

Here is the full code I have:

class AddFriendByUsernameTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {

    /**************************Global Variables************************/
    var friendObject = FriendClass()
    var friendsArray = [FriendClass]()
    var friendsUsernames = [String]()
    var isFirstLoading: Bool = true
    var utiltiies = Utilities()
    var searchActive : Bool = false
    var usernames:[String]!
    /**************************UI Components************************/
    @IBOutlet weak var searchBar: UISearchBar!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.searchBar.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func search(searchText: String? = nil){

        /****************************Get Username by Auth Data****************************/
        if(searchText != nil)
        {
            self.getAllUsersForSearchFilter({(result) -> Void in
                if(result.domain == "")
                {
                    let containsResult = self.usernames.contains(searchText!)
                    if(containsResult == true)
                    {
                        /*Query All information for found username*/
                        let reference = Firebase(url:"https://something.firebaseio.com/users/")
                        /****************************Get Username by Auth Data****************************/
                        reference.queryEqualToValue(searchText!).observeEventType(.Value, withBlock: { (snapshot: FDataSnapshot!) -> Void in

                            for userInstance in snapshot.children.allObjects as! [FDataSnapshot]{

              

Answers


Phillip Mills February 2016

To do some action when the keyboard Enter is tapped, you can create an IBAction method in your view controller, such as:

@IBAction func enterDetected(sender: UITextField) {
    print("Saw an 'Enter'")
}

Then connect the UITextField's "Primary Action Triggered" connection to the method in Interface Builder.

Post Status

Asked in February 2016
Viewed 3,362 times
Voted 10
Answered 1 times

Search




Leave an answer