Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which event get called when we hit UISearchBar

In my application, I need to do some activity i.e pushing otherview controller,when I click a UISearchbar which is added on view.

what is best approach to achive this.

As one of thing is when we click UISearchbar "searchBarTextDidBeginEditing" get fired,but with my scenario when I push view controller in "searchBarTextDidBeginEditing" and come back searchBarTextDidBeginEditing get called again, so seems it is not ideal place to push view controller.

This is maincontroller

// Search bar
  iSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
  iSearchBar.delegate = self;
  iSearchBar.showsCancelButton = NO;
  iSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
  iSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  [self addSubview:iSearchBar];

when I click UISearchBar then it calls

   - (void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar
   {
   [self ShowMySearch];
   }

In ShowMySearch , I am pushing some other controller lets say searchcontroller and when pop this searchcontroller and come back to maincontroller "searchBarTextDidBeginEditing" get call again and searchcontroller is pushed again and causing issue. this behavior is seen only on 3.1.1

Thanks,

Sagar

like image 756
Sagar... Avatar asked Aug 25 '10 12:08

Sagar...


2 Answers

I think calling [self ShowMySearch] in "searchBarTextDidBeginEditing" is a bit too late. I suppose that "searchBarTextDidBeginEditing" is called on response to the search bar becoming first responder. Since it is the first responder when the search controller is pushed, it probably become first responder again when your search controller is poped out...thus calling "searchBarTextDidBeginEditing" once again.

To achieve this, I'd use :

  • (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

This method is called after the search bar is tapped but before it becomes the first responder. And if you return NO, it will never become the first responder :

- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
    [self ShowMySearch];
    return NO;
}

Let me know if this works !

like image 51
Eric MORAND Avatar answered Nov 07 '22 22:11

Eric MORAND


For Swift 5.

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        handleShowSearchVC()
        return false
    }

    @objc func handleShowSearchVC() {
        let modalUserSearchController = UserSearchController(collectionViewLayout: UICollectionViewFlowLayout())
        modalUserSearchController.modalPresentationStyle = .overCurrentContext

        //Mini app panel.
        //vc.view.frame = CGRectMake(0, vc.view.frame.size.height - 120, vc.view.frame.size.width, 120)

        //Present #1
        // present(modalUserSearchController, animated: true, completion: nil)

        //Presentation #2
        navigationController?.pushViewController(modalUserSearchController, animated: true)
    }
like image 2
Marlhex Avatar answered Nov 07 '22 23:11

Marlhex