MOBILE

Programming the iPhone : Standard Control Types (part 5) - Search Bars

8/4/2012 3:22:45 PM

5. Search Bars

Search bars gather user input and allow applications to respond to that input in real time. A simple example is using a search bar at the top of the screen to filter a list of records in a table view based on string pattern matching. To use a search bar, you must create an instance of UISearchBar and add it to your view. You must also set the delegate property of the UISearchBar instance to an object that implements the UISearchBarDelegate protocol. Each method in the UISearchBarDelegate protocol handles a different user action. For example, you can implement the searchBar:textDidChange: method to develop progressive searches (a.k.a., auto-complete searches), or you can choose to execute searches only when editing ends using the searchBarSearchButtonClicked: and searchBarTextDidEndEditing: methods.

The TileSearchViewController class is from an example application that filters colored tiles based on color names input by a user:

#import "TileSearchViewController.h"
#import "Board.h"

@implementation TileSearchViewController

- (void)viewDidLoad
{
	searchBar = [[UISearchBar alloc] 
		initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
	searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
	searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
	searchBar.showsCancelButton = NO;

	//	Set myself as the delegate of the search bar.
	searchBar.delegate = self;
	
	[self.view addSubview:searchBar];
	if(board == nil) board = [[Board alloc]
		initWithFrame:CGRectMake(0.0, 45.0, 320.0, 415.0)];
	[self.view addSubview:board];
}


#pragma mark UISearchBarDelegate methods
- (void)searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar
{
	//	Show cancel button while editing
	searchBar.showsCancelButton = YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)theSearchBar
{
	//	Hide cancel button when editing ends
	searchBar.showsCancelButton = NO;
}

- (void)searchBar:(UISearchBar *)theSearchBar
	textDidChange:(NSString *)searchText
{
	[board filterForColorName:searchText];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)theSearchBar
{
	//	Repopulate the last color unless the user cleared the search
	if (theSearchBar.text.length > 0){
		searchBar.text = lastSearch;
	}else{
		searchBar.text = @"";
	}
	
	[searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
	[lastSearch release];
	lastSearch = [theSearchBar.text copy];
	[searchBar resignFirstResponder];
}


- (void)dealloc
{
	[searchBar release];
	[board release];
	[lastSearch release];
	[super dealloc];
}

@end


					  

Figure 9 shows a screenshot of the TileSearch example application.

Figure 9. The TileSearch example application


Other  
  •  Sony Introduced 3 New Phones In Xperia Series: Tipo, Miro And Ion
  •  Samsung Galaxy SIII And HTC One X: A Detailed And Thorough Comparison
  •  In Control
  •  BlackBerry Virtual Keyboard: The Development Through Each Appliance
  •  3D Phone – Why Is LG Optimus 3D Max P725 The Best Choice, Still?
  •  XNA Game Studio 4.0 : Xbox 360 Gamepad (part 2) - Moving Sprites Based on Gamepad Input
  •  XNA Game Studio 4.0 : Xbox 360 Gamepad (part 1) - Reading Gamepad State
  •  XNA Game Studio 4.0 : Adding Interactivity with User Input - Precision Control of a Mouse
  •  Asus Transformer Pad TF300
  •  Mobile Phone Game Programming : Using Sprite Animation - Building the UFO Example Program
  •  Mobile Phone Game Programming : Using Sprite Animation - Achieving Smooth Animation with the GameCanvas Class
  •  Mobile Phone Game Programming : Using Sprite Animation - Working with the Layer and Sprite Classes
  •  Windows Phone 8 Unveiled
  •  iOS 6 Beta Review (Part 2)
  •  iOS 6 Beta Review (Part 1)
  •  Ipad : Tabletop
  •  Ipad Lion (Part 3) - Other possible features
  •  Ipad Lion (Part 2) - What to expect from the upcoming mountain lion
  •  Ipad Lion (Part 1) - Lion paving the way for mountain lion's destiny
  •  The Effect Of IOS And Facebook On Shutterbugs (Part 2)
  •  
    Most View
    Seagate Wireless Plus 1TB - Seagate's Second Wireless External Hard Drive
    Programming .NET Components : Serialization Events (part 2) - Serialization Events and Class Hierarchies, Serialization and Versioning
    Manage iOS with iCloud (Part 1)
    2013: TV Gets Sharper, Smarter And More Local
    Simplicity: intuitive design, simple choices, and familiarity
    Audeze LCD-2 Headphones At Music Direct (Part 2)
    Samsung Galaxy Beam - Stay For The Projector (Part 2)
    Graphics Card Shootout - Budget FPS (Part 4)
    ASROCK FM2A75 Pro4-M - Limited Performance
    The Complete Guide To Garageband (Part 3)
    Top 10
    Nexus 4 – A Smartphone With Elegant Design (Part 7)
    Nexus 4 – A Smartphone With Elegant Design (Part 6)
    Nexus 4 – A Smartphone With Elegant Design (Part 5)
    Nexus 4 – A Smartphone With Elegant Design (Part 4)
    Nexus 4 – A Smartphone With Elegant Design (Part 3)
    Nexus 4 – A Smartphone With Elegant Design (Part 2)
    Nexus 4 – A Smartphone With Elegant Design (Part 1)
    Sony Nex-5R – A Camera With Improved Performance And Focusing (Part 3)
    Sony Nex-5R – A Camera With Improved Performance And Focusing (Part 2)
    Sony Nex-5R – A Camera With Improved Performance And Focusing (Part 1)