Yohan Dahmani
Yohan Dahmani

Reputation: 1928

UISearchBar SearchButtonClicked never called?

I am new to iOS, and I am using Xamarin Studio on MacOS (I already have my Xamarin.Droid app and now making it on iOS).

I am struggling using an UISearchBar, I want a really basic usage tho .. I tried to follow this recipes (https://developer.xamarin.com/recipes/ios/content_controls/search-controller/) but still can't manage to make it work.

What I am trying to get is a SearchBar, in which the user should type text then press the "search" button on the keyboard. Pressing the search button should call code that will populate an UITableView with data retrieved on my webservice.

Here's my Storyboard structure, a ViewController which contains an UITableView with a name

My ViewController

Structure

ViewController Properties

TableView Properties

I tried the following code, everything seems to work fine, but when I press search on the keyboard, SearchButtonClicked event is never fired ... And I can't figure out why

public partial class SearchViewController : UIViewController
    {
        public SearchViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            var searchController = new UISearchController(this);

            searchController.SearchBar.SizeToFit();
            searchController.SearchBar.ShowsCancelButton = true;
            searchController.SearchBar.SearchButtonClicked += delegate {
                Console.WriteLine("Hello ????");
                this.Title = "This code is never called";
            };


            RunnersSearchTableView.TableHeaderView = searchController.SearchBar;

            base.ViewDidLoad();
        }
    }

Also tried

public partial class SearchViewController : UIViewController
    {
        public SearchViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            var searchController = new UISearchController(this);

            searchController.SearchBar.SizeToFit();
            searchController.HidesNavigationBarDuringPresentation = true; 
            searchController.SearchBar.ShowsCancelButton = true;
            searchController.SearchBar.Delegate = new MySearchBarDelegate();

            RunnersSearchTableView.TableHeaderView = searchController.SearchBar;

            base.ViewDidLoad();
        }
    }

    public class MySearchBarDelegate : UISearchBarDelegate
    {
        public MySearchBarDelegate()
        {

        }

        public override void SearchButtonClicked(UISearchBar searchBar)
        {
            Console.WriteLine("TEST");
            searchBar.ResignFirstResponder();
            base.SearchButtonClicked(searchBar);
        }
    }

And also tried

public partial class SearchViewController : UIViewController
    {
        public SearchViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            var searchController = new UISearchController(this);

            searchController.SearchBar.SizeToFit();
            searchController.HidesNavigationBarDuringPresentation = true;
            searchController.SearchBar.ShowsCancelButton = true;
            searchController.SearchBar.WeakDelegate = this;

            RunnersSearchTableView.TableHeaderView = searchController.SearchBar;


            base.ViewDidLoad();
        }

        [Export("searchBarSearchButtonClicked:")]
        public virtual void SearchButtonClicked(UISearchBar searchBar)
        {
            Console.WriteLine("hello??");
            searchBar.ResignFirstResponder();
        }
    }

None of them seems to work

Upvotes: 1

Views: 370

Answers (1)

KKRocks
KKRocks

Reputation: 8322

You need to register delegate for searchbar .

[Foundation.Register("UISearchBar", true)]

for more : https://developer.xamarin.com/api/type/UIKit.UISearchBar/

Upvotes: 1

Related Questions