title: iOS: How to implement search controller using objective c & How to call API at runtime i.e when I start typing in search controller?
body: <p>I want to implement search controller like gmail application,</p>
  
  <p><a href="https://i.stack.imgur.com/T9te6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T9te6.jpg" alt="enter image description here"></a></p>
  
  <p><strong>OR</strong> I want to implement like this example</p>
  
  <p><a href="https://i.stack.imgur.com/hPv4N.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hPv4N.gif" alt="enter image description here"></a></p>
  
  <p><strong>Image 1</strong> It shows normal inbox with search controller button</p>
  
  <p><strong>Image 2</strong> After clicking on search button It will expand show like image 2. Here we can enter the words...etc</p>
  
  <p><strong>Image 3</strong> Here whatever we enter, depending upon text it shows in list, and when we click on it, it will navigate to detail view.</p>
  
  <p>same thing I want to implement.</p>
  
  <p>Only thing is that, when I will start typing in search controller, one API will call i.e search, then depending upon text entered in search, it will take as parameter and it will show data in list (table view). this data will come from API response.</p>
  
  <p>And when I click on any result, it has to move detail view.</p>
  
  <p>Anybody, can help me.</p>
  
  <p>I am totally new in iOS. Just now I started learning of iOS. Plz help me for this.</p>
  
  <p>Here is .h file</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface PDSearchExampleTableViewController : UITableViewController
  {
      BOOL searching;
  }
  
  @property (strong, nonatomic) UISearchBar *searchBar;
  @property (strong, nonatomic) NSMutableArray *sampleDataArray;
  @property (strong, nonatomic) NSMutableArray *filteredSampleDataArray;
  
  
  - (IBAction)searchButtonClicked:(id)sender;
  
  @end
  </code></pre>
  
  <p>Here is .m file</p>
  
  <pre><code>#import "PDSearchExampleTableViewController.h"
  
  @interface PDSearchExampleTableViewController () &lt;UISearchDisplayDelegate, UISearchBarDelegate&gt;
  
  @end
  
  @implementation PDSearchExampleTableViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
  
      _searchBar.delegate = self;
      _sampleDataArray = [[NSMutableArray alloc] init];
      _filteredSampleDataArray = [[NSMutableArray alloc] init];
      [_sampleDataArray addObject:@"one"];
      [_sampleDataArray addObject:@"two"];
      [_sampleDataArray addObject:@"three"];
      [_sampleDataArray addObject:@"four"];
      [_sampleDataArray addObject:@"five"];
      [_sampleDataArray addObject:@"six"];
      [_sampleDataArray addObject:@"seven"];
  
  
  
       [self.tableView reloadData];
  }
  
  - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  #pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      // Return the number of sections.
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      // Return the number of rows in the section.
      if (searching) {
          return [_filteredSampleDataArray count];
      } else {
          return [_sampleDataArray count];
      }
  }
  
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sampleSearchCell" forIndexPath:indexPath];
  
  
      if (searching) {
          cell.textLabel.text = [_filteredSampleDataArray objectAtIndex:indexPath.row];
      } else {
          cell.textLabel.text = [_sampleDataArray objectAtIndex:indexPath.row];
      }
  
      return cell;
  }
  
  - (IBAction)searchButtonClicked:(id)sender {
      self.navigationItem.rightBarButtonItem = nil;
      _searchBar = [[UISearchBar alloc] init];
      _searchBar.delegate = self;
      _searchBar.placeholder = @"Search Sample Data";
      [_searchBar sizeToFit];
      self.navigationItem.titleView = _searchBar;
      [_searchBar becomeFirstResponder];
      [_searchBar.window makeKeyAndVisible];
  }
  
  - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
      [_searchBar setShowsCancelButton:YES animated:YES];
  }
  
  - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
  
  }
  
  - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
      searching = NO;
      [self.tableView reloadData];
      self.navigationItem.titleView = nil;
      UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchButtonClicked:)];
      [_searchBar setShowsCancelButton:NO];
      [_searchBar resignFirstResponder];
      self.navigationItem.rightBarButtonItem = rightBarButton;
  }
  
  - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
      [_filteredSampleDataArray removeAllObjects];
  
      if ([searchText length] != 0) {
          searching = YES;
          [self searchData];
      } else {
          searching = NO;
      }
  
      [self.tableView reloadData];
  }
  
  - (void)searchData {
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", _searchBar.text];
      NSArray *tempArray = [_sampleDataArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@", tempArray);
      _filteredSampleDataArray = [NSMutableArray arrayWithArray:tempArray];
  }
  </code></pre>