在iOS中触摸屏时如何隐藏键盘
我正在在iOS中触摸屏时如何隐藏键盘,ios,objective-c,xcode,uisearchbar,Ios,Objective C,Xcode,Uisearchbar,我正在iOS中创建搜索栏,当我按下搜索栏时,键盘将出现,但搜索完成后,我希望键盘将消失在屏幕上。 我正在实现代码,但它不是隐藏的 //this methos show to create dynamic search bar -(void)SearchBarCode { //[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
iOS
中创建搜索栏,当我按下搜索栏时,键盘将出现,但搜索完成后,我希望键盘将消失在屏幕上。
我正在实现代码,但它不是隐藏的
//this methos show to create dynamic search bar
-(void)SearchBarCode
{
//[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
self.disableViewOverlay = [[UIView
alloc]initWithFrame:CGRectMake(0.0f,44.0f,320.0f,416.0f)];
self.disableViewOverlay.backgroundColor=[UIColor blackColor];
self.disableViewOverlay.alpha = 0;
theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(-61, 46, 378.0f, 50)];
theSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 46, 310.0,
44.0)];
searchBarView.autoresizingMask = 0;
theSearchBar.delegate =self;
[searchBarView addSubview:theSearchBar];
self.navigationItem.titleView=searchBarView;
scrollView.backgroundColor = [UIColor brownColor];
theSearchBar.placeholder = @"Search";
theSearchBar.delegate = self;
}
- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar becomeFirstResponder];
}
- (void)viewDidAppear:(BOOL)animated
{
[self.theSearchBar resignFirstResponder];
[super viewDidAppear:animated];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[theSearchBar setShowsCancelButton:NO animated:NO];
[theSearchBar becomeFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
theSearchBar.text = @"";
[theSearchBar setShowsCancelButton:YES animated:YES];
[theSearchBar resignFirstResponder];
}
- (void)searchTableList
{
NSString *searchString = theSearchBar.text;
filteredContentList = [[NSMutableArray alloc]init];
for (SMSCategory *cat in Arrayobject)
{
NSString *tempStr = cat.Name;
NSComparisonResult result = [tempStr compare:searchString options:
(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0,
[searchString length])];
if (result == NSOrderedSame)
{
[filteredContentList addObject:cat];
}
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
}
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active
{
self.theTableView.allowsSelection = !active;
self.theTableView.scrollEnabled = !active;
if (!active)
{
[disableViewOverlay removeFromSuperview];
[theSearchBar resignFirstResponder];
}
else
{
self.disableViewOverlay.alpha = 0;
[self.view addSubview:self.disableViewOverlay];
[UIView beginAnimations:@"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
self.disableViewOverlay.alpha = 0.6;
[UIView commitAnimations];
// probably not needed if you have a details view since you
// will go there on selection
NSIndexPath *selected = [self.theTableView
indexPathForSelectedRow];
if (selected)
{
[self.theTableView deselectRowAtIndexPath:selected
animated:NO];
}
}
[theSearchBar setShowsCancelButton:active animated:YES];
}
.h文件代码
@interface CategoryMainWindowViewController : UIViewController<UISearchBarDelegate,UITextViewDelegate>{
NSMutableArray *Arrayobject;
UIView *disableViewOverlay;
UISearchBar *theSearchBar;
NSMutableArray *filteredContentList;
BOOL isSearching;
}
@property(retain) UIView *disableViewOverlay;
@property(retain) NSMutableArray *Arrayobject;
@property (nonatomic, retain) IBOutlet UITableView *theTableView;
@property (nonatomic, retain) IBOutlet UISearchBar *theSearchBar;
@property(nonatomic,retain) UIScrollView *scrollView;
@property(strong,nonatomic) NSString *databasePath;
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active;
- (void)DynamicButton:(NSMutableArray*)objectName;
- (NSMutableArray*)GetData;
@end
@接口类别MainWindowViewController:UIViewController{
NSMutableArray*阵列对象;
UIView*禁用视图覆盖;
ui搜索栏*搜索栏;
NSMutableArray*filteredContentList;
布尔正在研究;
}
@属性(保留)UIView*disableViewOverlay;
@属性(保留)NSMutableArray*Arrayobject;
@属性(非原子,保留)IBUITableView*TableView;
@属性(非原子,保留)IBUI搜索栏*搜索栏;
@属性(非原子,保留)UIScrollView*scrollView;
@属性(强,非原子)NSString*数据库路径;
-(无效)搜索栏:(UISearchBar*)搜索栏激活:(BOOL)激活;
-(void)DynamicButton:(NSMutableArray*)objectName;
-(NSMutableArray*)获取数据;
@结束
将此方法添加到视图中
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touching on screen");
[self.view endEditing:YES];
}
在搜索
发生后,您应该调用键盘
方法
- (void) dismissKeyboard
{
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
这不能工作,因为“becomeFirstResponder”显示键盘而不是隐藏它!hide命令将是resignFirstResponder,如下所示:
- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
[self dismissKeyboard];
}
编辑:
如果您只想在用户搜索时隐藏键盘,那么只需实现UISearchBarDelegate-方法就更容易了
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
然后从那里调用[self dismissKeyboard],如下所示:
- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
[self dismissKeyboard];
}
检查becomeFirstResponder和resignFirstResponder的组合。我能知道原因吗?当我点击搜索栏时,键盘会出现,但之后我触摸屏幕,它不会隐藏在你调用dismissKeyBoard方法的地方。你是否在主视图中添加了任何滚动视图或其他视图?如果是,则这将不起作用。问题是,当我运行我的应用程序时,键盘已经出现。我希望当我点击搜索栏时键盘出现,我建议你删除所有becomeFirstResponder和辞职FirstResponder,除了你的解雇键盘方法。然后用我在回答中建议的代码替换您的代码,然后重试。并移除手势识别器。但有一个问题是,我的标题没有显示,我在主屏幕stroyboard中设置了我的标题。问题是什么?你的标题是什么意思?指windows标题,如SMS集合和所有