gpt4 book ai didi

ios - 如何在iOS中触摸屏时隐藏键盘

转载 作者:行者123 更新时间:2023-12-01 18:56:34 26 4
gpt4 key购买 nike

我按搜索栏时会在search bar中创建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

最佳答案

这不能工作,因为“becomeFirstResponder”显示键盘而不是隐藏键盘! hide-命令将是resignFirstResponder,如下所示:

- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}

编辑:
如果您唯一想做的就是在用户搜索后隐藏键盘,则只需实现 UISearchBarDelegate -方法会更容易
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;

然后从此处调用[self dismissKeyboard],如下所示:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
[self dismissKeyboard];
}

关于ios - 如何在iOS中触摸屏时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com