1

I currently have an app that contains a list of items, I now want to be able to search that list. I know Ill need to use a Search Bar and Controller, the only thing is, I can't find any documentation or examples for implementing this. I have a controller class setup for the search bar, but its a blank class. Where is a good starting point for this?

This question seems to be a good spot, but where does that go, and how do I port that to C# for xamarin?

Community
  • 1
  • 1
hightekjonathan
  • 551
  • 1
  • 8
  • 29

3 Answers3

3

Xamarin 5.10:

    var sampleSearchBar = new UISearchBar (new CoreGraphics.CGRect (20, 20, this.View.Frame.Width - 50, 40));
sampleSearchBar.SearchBarStyle = UISearchBarStyle.Prominent;

 sampleSearchBar.ShowsCancelButton = true;

    //Deleagte class source
  sampleSearchBar.Delegate = new SearchDelegate ();
 this.View.AddSubview (sampleSearchBar);

Add Delegate class to the ViewController.

class SearchDelegate : UISearchBarDelegate
        {
            public override void SearchButtonClicked (UISearchBar bar)
            {
                bar.ResignFirstResponder ();
            }

            public override void CancelButtonClicked (UISearchBar bar)
            {
                bar.ResignFirstResponder ();
            }

            public override bool ShouldBeginEditing (UISearchBar searchBar)
            {

                return true;
            }

            public override bool ShouldEndEditing (UISearchBar searchBar)
            {
                return true;
            }

            public override bool ShouldChangeTextInRange (UISearchBar searchBar, NSRange range, string text)
            {
                Console.WriteLine (searchBar.Text);
                return true;
            }
        }
A.G
  • 13,048
  • 84
  • 61
1

This is an iOS sample application that demonstrates how to use UISearchController. A search controller manages the presentation of a search bar (in concert with the results view controller’s content) Look at this and let me know if any issue after that. https://github.com/xamarin/monotouch-samples/tree/master/ios8/TableSearch

Sport
  • 6,972
  • 6
  • 38
  • 61
  • Im actually looking at that currently, but they add the search bar programmatically, and I want it in the storyboard. – hightekjonathan Jul 05 '15 at 03:30
  • so just give the name of search bar after creating outlet it will work – Sport Jul 05 '15 at 03:32
  • Do I then set the delegate to the SearchController? – hightekjonathan Jul 05 '15 at 03:32
  • yes must UISearchBarDelegate , see https://github.com/xamarin/monotouch-samples/tree/master/SearchDemo – Sport Jul 05 '15 at 03:34
  • Im sorry, but that doesn't make sense to me. I don't understand why the SearchController inherits the BaseController. Is there a way to do this without having to have more than 1 file? – hightekjonathan Jul 05 '15 at 03:46
  • According to this, UISearchDisplayController is deprecated, so use UISearchController instead http://stackoverflow.com/a/25826374/812013. And as far as being able to add it in to your Storyboard, this is not yet possible and the deprecated search control still resides there, according to this http://stackoverflow.com/a/26689445/812013. Therefore you must add the new UISearchController programatically. – Chucky May 24 '16 at 10:54
1

I create this demo for you with Storyboard and UISearchDisplayControlelr.

Look at this SearchDemo.

Lei Kan
  • 457
  • 6
  • 20
  • This works perfectly except for I also need to search from a detailTextLabel in addition to the normal label, and upon clicking on a searched item, I need to push the view to the detail view controller, which doesn't happen. I downloaded your example from GitHub, and when running, it throws an exception when trying to search, or click a item. – hightekjonathan Jul 07 '15 at 07:04
  • @hightekjonathan search from a detailTextLabel is easy to achieve. I have updated that demo. What's the detail message of the exception? It works well on my iPhone 6 iOS 8.3 simulator. – Lei Kan Jul 07 '15 at 08:25
  • I am unable to check at this moment. I believe it was a NSOutOfRangeException. I'll take a look in the morning – hightekjonathan Jul 07 '15 at 08:26