[iPhone] UISearchBar の使い方メモ
iPhone アプリで検索機能に UISearchBar を使用したときのメモです。
SearchBar という名前のプロジェクトを ViewBased で作成したとして書きます。
UISearchBar を配置
IB で SearchBarView に UISearchBar を配置します。配置した UISearchBar を右クリックして delegate と File’s Owner を接続しておきます。これでUISearchBar のイベントが SearchBarViewController: に通知されます。
SearchBarViewContoroller.m
検索実行したときは searchBarSearchButtonClicked: が呼ばれます。インクリメンタルサーチなどをしたい場合は UISearchBar の文字が変更されるたびに searchBar: が呼ばれます。
searchBarSearchButtonClicked: の中で [searchBar resignFirstResponder]; としているのは検索実行後にキーボードを隠すためです。
- (void) searchItem:(NSString *) searchText { // 検索処理 } - (void) searchBarSearchButtonClicked: (UISearchBar *) searchBar { [searchBar resignFirstResponder]; [self searchItem:searchBar.text]; } - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *) searchText { NSLog(@"serch text=%@", searchText); if ([searchText length]!=0) { // インクリメンタル検索など } }
ブックマークボタンとキャンセルボタン
IB で UISearchBar の属性を変えることにより、検索バーにブックマークボタンやキャンセルボタンを表示することができます。それぞれ、searchBarBookmarkButtonClicked: 、searchBarCancelButtonClicked: で受け取れます。
UISearchBar のその他の属性
autocapitalizationType
先頭の文字を自動的に大文字にするか。デフォルトは大文字になります。
keyboardType
最初に表示するキーボードのタイプを指定できます。
関連する投稿
This entry was posted by matsuura on 1月 29, 2009 at 8:58 pm, and is filed under iPhone/iPod touch. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.
One comment
コメントをどうぞ コメントをキャンセル
Additional comments powered by BackType
3bristol…
…