User1075
User1075

Reputation: 885

How to fire UISearchBar delegate single time?

I am filtering an array while the user types the text in uisearchbar but the problem is that i have got an alert handler which fires every time the delegate is called, but i want that the alert comes only single time not multiple times... The code is below

     -(void)searchBar:(UISearchBar )searchBar textDidChange:(NSString )searchText {



    if(searchText.length == 0)
    {
        _isFiltered = FALSE;
    }
    else{
        // NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"(SELF.name contains[cd] %@)or(SELF.rank contains[cd] %@)or(SELF.score contains[cd] %@)",searchText,searchText,searchText];
        NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"(SELF contains[cd] %@)",searchText];
        self.filteredTableData = [self.searchItemsArray filteredArrayUsingPredicate:resultPredicate];
        NSLog(@"Predicated array %@", self.filteredTableData);
        self.isFiltered = YES;
        if (self.filteredTableData.count == 0) {
            [[CLAlertHandler standardAlertHandler]showAlert:@"No match found!" title:AppName];
            [self.searchTableView reloadData];


        }

    }
    //[self.searchTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    [self.searchTableView reloadData];
}

Upvotes: 1

Views: 51

Answers (2)

Vinod Vishwanath
Vinod Vishwanath

Reputation: 5891

Why not have a boolean variable to determine whether the alert has been shown before?

@interface ViewController() { //extension in .m file

BOOL noResultsAlertShown;

}

Then

if (self.filteredTableData.count == 0 && !noResultsAlertShown) {

     noResultsAlertShown = YES;

     [[CLAlertHandler standardAlertHandler]showAlert:@"No match found!" title:AppName];
     [self.searchTableView reloadData];

}

Upvotes: 0

Aderstedt
Aderstedt

Reputation: 6518

Every time the delegate method is called, use -[NSNotification enqueueNotification…] to post a notification to yourself, with priority NSPostWhenIdle (for example) and a coalesce mask. This will merge these notifications and fire them when the system is idle, in this case letting you do work when the user isn't directly typing.

In your implementation file:

const NSString *myNotificationName = @"kMyNotificationName";

In viewWillAppear, or similar:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleSearchFieldUpdate:)
                                             name:myNotificationName object:self];

In the delegate method:

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:myNotificationName object:self]
                                           postingStyle:NSPostWhenIdle
                                           coalesceMask:NSNotificationCoalescingOnName
                                               forModes:nil];

As a new class method:

- (void)handleSearchFieldUpdate:(NSNotification *)notification {
    .. do your work ..
}

The end result will be that your selector handleSearchFieldUpdate is called every time the user stops typing.

Upvotes: 2

Related Questions