方法一
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
// Do the search...
}
方法二
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Other way (enumerating over all text views):
Here's a step by step for it:
Add an IBAction to your view controller, such as - (IBAction)backgroundTouch:(id)sender
In the backgroundTouch action, you need to send the resignFirstResponder message to all of the text boxes in your view. This is unfortunate but necessary since there's currently no way to retrieve the object that currently has FirstResponder status. It should look something like this:
- (IBAction)backgroundTouch:(id)sender {
[someTextBox resignFirstResponder];
[anotherTextBox resignFirstResponder];
}
方法三
當直接使用searchBar+tableView會無法點出去
因為怎麼點都是在同一個物件內
這時候要搭配一個subview擋在searchBar+tableView前
讓使用者可以點到其他物件
所以先建立兩個事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil];
-(void)keyboardShown:(NSNotification *)note
{
subView.hidden=NO;
}
-(void)keyboardHidden:(NSNotification *)note{
subView.hidden=YES;
}
沒有留言:
張貼留言