1. 程式人生 > >iOS--UISearchBar 搜尋框 的使用方法詳細

iOS--UISearchBar 搜尋框 的使用方法詳細

 // UISearchBar的常用方法 搜尋框

    UISearchBar *oneSearchBar = [[UISearchBar allocinit];

    oneSearchBar.frame = CGRectMake(0032070); // 設定位置和大小

    oneSearchBar.keyboardType = UIKeyboardTypeEmailAddress; // 設定彈出鍵盤的型別

    oneSearchBar.barStyle = UIBarStyleBlackTranslucent; // 設定UISearchBar的樣式

    oneSearchBar.

tintColor = [UIColor redColor]; // 設定UISearchBar的顏色 使用clearColor就是去掉背景

    oneSearchBar.placeholder = @"請輸入:"// 設定提示文字

    oneSearchBar.text = @"呵呵"// 設定預設的文字

    oneSearchBar.prompt = @"提示資訊"// 設定提示

    oneSearchBar.delegate = self// 設定代理

    oneSearchBar.showsCancelButton = YES// 設定時候顯示關閉按鈕

 // oneSearchBar.showsScopeBar = YES; // 設定顯示範圍框

 // oneSearchBar.showsSearchResultsButton = YES; // 設定顯示搜尋結果

 // oneSearchBar.showsBookmarkButton = YES; // 設定顯示書籤按鈕

    [self.view addSubview:oneSearchBar]; // 新增到View

    [oneSearchBar release], oneSearchBar = nil// 釋放記憶體

////////////////////////一些常用的代理方法//////////////////////////////

#pragma mark - 實現取消按鈕的方法

- (void)searchBarCancelButtonClicked:(UISearchBar

 *)searchBar {

 NSLog(@"您點選了取消按鈕");

    [searchBar resignFirstResponder]; // 丟棄第一使用者

}

#pragma mark - 實現鍵盤上Search按鈕的方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

 NSLog(@"您點選了鍵盤上的Search按鈕");

}

#pragma mark - 實現監聽開始輸入的方法

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

 NSLog(@"開始輸入搜尋內容");

 return YES;

}

#pragma mark - 實現監聽輸入完畢的方法

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {

    NSLog(@"輸入完畢");

 return YES;

}