点击当前页面搜索框跳转下个页面搜索框并且获取焦点

当前页面搜索框回调处理事件跳转页面之前的搜索框代理

 - (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"开始编辑");
    [search resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if(search==textField){
        SsSearchLocation *vc=[[SsSearchLocation alloc]init];
        vc.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    return YES;
}

跳转之后搜索框页面处理

-(void)cancelBtn{
    //取消回到上一界面
    [self.navigationController popViewControllerAnimated:NO];
    
//    [self dismissViewControllerAnimated:NO completion:nil];
}
-(void)CreatSearchView{
    //隐藏导航栏上的返回按钮
    [self.navigationItem setHidesBackButton:YES];
    self.navigationItem.hidesBackButton = YES;
    //用来放searchBar的View
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, S_WIDTH, 30)];
    //创建searchBar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 20, 30)];
    //默认提示文字
    searchBar.placeholder = @"搜索地址";
    //背景图片
    searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"];
    //代理
    searchBar.delegate = self;
    //显示右侧取消按钮
    searchBar.showsCancelButton = YES;
    //光标颜色
    searchBar.tintColor = UIColorFromRGB(0x595959);
    //拿到searchBar的输入框
    _searchTextField = [searchBar valueForKey:@"_searchField"];
    //字体大小
    _searchTextField.font = [UIFont systemFontOfSize:15];
    //输入框背景颜色
    _searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
    
    //拿到取消按钮
    UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"];
    //设置按钮上的文字
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    //设置按钮上文字的颜色
    [cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [cancleBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:searchBar];
    [searchBar becomeFirstResponder];
    self.searchBar = searchBar;
    self.navigationItem.titleView = titleView;
    
   
    
    
    
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//    [self creatAFNetworking];
    
    NSLog(@"SearchButton");
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    _SearchText = searchText;
    NSLog(@"-------%@",_SearchText);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

发现个问题,pop的时候添加两次会直接跳回上上级页面。

具体操作:

 -(void)cancelBtn{
    //取消回到上一界面
    [self.navigationController popViewControllerAnimated:NO];
    
//    [self dismissViewControllerAnimated:NO completion:nil];
}

2.这里也添加回调之后触发两次pop回上上级页面

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
  [self.navigationController popViewControllerAnimated:NO];
}

你可能感兴趣的:(点击当前页面搜索框跳转下个页面搜索框并且获取焦点)