locked
TouchUpInside event is not trigger RRS feed

  • Question

  • User383304 posted

    Hi guys,

    I have added SearchController to my iOS app but I want to achieve something a little bit complicated. When I type in something in the search bar I do not want to filter the table, I want to show a custom view with some search result results (something like when you search for something in the Amazon search bar). I haven't been able to find a component with the same behaviour as Amazon search bar so I started to do something on my own (if you guys have any idea of such component, let me know).

    Meanwhile, I have achieved something, i post it here. The question is: after the custom view from AddSearchBarToTopNavigationItem() method is added, the TouchUpInside event from the uiButton is now working. Any idea why ?

    `public override void ViewDidLoad() { base.ViewDidLoad();

            table = new UITableView(View.Bounds)
            {
                AutoresizingMask = UIViewAutoresizing.All,
                RowHeight = UITableView.AutomaticDimension,
                EstimatedRowHeight = 70,
                SeparatorStyle = UITableViewCellSeparatorStyle.None
            };
    
            table.Source = CustomSource.Items;
            table.ReloadData();
    
            Add(table);
            AddSearchBarToTopNavigationItem();
        }
        private void AddSearchBarToTopNavigationItem()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                var searchController = new UISearchController(searchResultsController: null)
                {
                    HidesNavigationBarDuringPresentation = true,
                    DimsBackgroundDuringPresentation = false,
                    ObscuresBackgroundDuringPresentation = true
                };
    
                searchController.SearchBar.SearchBarStyle = UISearchBarStyle.Minimal;
                searchController.SearchBar.Placeholder = "Empty";
                searchController.SearchBar.TintColor = Colors.Red.ToUIColor();
                searchController.SearchBar.BarTintColor = Colors.Black.ToUIColor();
    
                this.NavigationItem.SearchController = searchController;
                this.NavigationItem.HidesSearchBarWhenScrolling = true;
    
                this.NavigationItem.SearchController.SearchBar.TextChanged += async (sender, e) =>
                {
                    if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
                    cancellationTokenSource = new CancellationTokenSource();
                    var cancellationToken = cancellationTokenSource.Token;
    
                    var searchBar = sender as UISearchBar;
                    if (searchBar != null && searchBar.Text.Length >= 3)
                    {
                        string searchText = searchBar.Text;
                        try
                        {
                            if (cancellationToken.IsCancellationRequested) return;
    
    
                            var uiButton = new UIButton(new CoreGraphics.CGRect(20, 100, this.View.Frame.Width, 20))
                            {
                                BackgroundColor = UIColor.Black;
                            };
                            uiButton.SetTitle("Text", UIControlState.Normal);
                            uiButton.TouchUpInside += UiButton_TouchUpInside;
    
    
                            view.Add(uiButton);
    
                            view.BackgroundColor = Colors.BordersLightGrey.ToUIColor();
                            this.View.AddSubview(view);
                        }
                        catch (OperationCanceledException)
                        {
                            // Expected
                        }
                        catch (Exception ex)
                        {
                            //Logger.Error(ex);
                        }
                    }
                };
            }
        }
    

    `

    Wednesday, April 22, 2020 1:03 PM

Answers

  • User369978 posted

    You don't set frame on view , please notice the area of the view should be larger than the button .

     view = new UIView();
     view.Frame = xxxx;
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 23, 2020 3:10 PM

All replies

  • User369978 posted

    Where you instantiate view ? Could you post that part of code ?

    I assume the button beyond the view's area , you touch on the incorrect position , so the TouchUpInside event does not trigger .

    Wednesday, April 22, 2020 2:25 PM
  • User383304 posted

    @ColeX the view is show but I add the button as a subview to view.

    This is the entire code:

    `using System; using UIKit; using System.Threading; using System.Threading.Tasks;

    namespace customapp { public partial class ViewController : UiViewController { UIView view; CancellationTokenSource cancellationTokenSource;

        public ViewController(IntPtr handle) : base(handle)
        {
            cancellationTokenSource = new CancellationTokenSource();
        }
    
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            view = new UIView();
    
            table = new UITableView(View.Bounds)
            {
                AutoresizingMask = UIViewAutoresizing.All,
                RowHeight = UITableView.AutomaticDimension,
                EstimatedRowHeight = 70,
                SeparatorStyle = UITableViewCellSeparatorStyle.None
            };
    
            table.Source = CustomSource.Items;
            table.ReloadData();
    
            Add(table);
            AddSearchBarToTopNavigationItem();
        }
        private void AddSearchBarToTopNavigationItem()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                var searchController = new UISearchController(searchResultsController: null)
                {
                    HidesNavigationBarDuringPresentation = true,
                    DimsBackgroundDuringPresentation = false,
                    ObscuresBackgroundDuringPresentation = true
                };
    
                searchController.SearchBar.SearchBarStyle = UISearchBarStyle.Minimal;
                searchController.SearchBar.Placeholder = "Empty";
                searchController.SearchBar.TintColor = Colors.Red.ToUIColor();
                searchController.SearchBar.BarTintColor = Colors.Black.ToUIColor();
    
                this.NavigationItem.SearchController = searchController;
                this.NavigationItem.HidesSearchBarWhenScrolling = true;
    
                this.NavigationItem.SearchController.SearchBar.TextChanged += async (sender, e) =>
                {
                    if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
                    cancellationTokenSource = new CancellationTokenSource();
                    var cancellationToken = cancellationTokenSource.Token;
    
                    var searchBar = sender as UISearchBar;
                    if (searchBar != null && searchBar.Text.Length >= 3)
                    {
                        string searchText = searchBar.Text;
                        try
                        {
                            if (cancellationToken.IsCancellationRequested) return;
    
    
                            var uiButton = new UIButton(new CoreGraphics.CGRect(20, 100, this.View.Frame.Width, 20))
                            {
                                BackgroundColor = UIColor.Black;
                            };
                            uiButton.SetTitle("Text", UIControlState.Normal);
                            uiButton.TouchUpInside += UiButton_TouchUpInside;
    
    
                            view.Add(uiButton);
    
                            view.BackgroundColor = UIColor.White;
                            this.View.AddSubview(view);
                        }
                        catch (OperationCanceledException)
                        {
                            // Expected
                        }
                        catch (Exception ex)
                        {
                            //Logger.Error(ex);
                        }
                    }
                };
            }
        }
    
        private void UiButton_TouchUpInside(object sender, EventArgs e)
        {
            var button = sender as UIButton;
            var text = button.CurrentTitle;
        }
    }
    

    }

    `

    Wednesday, April 22, 2020 2:45 PM
  • User369978 posted

    You don't set frame on view , please notice the area of the view should be larger than the button .

     view = new UIView();
     view.Frame = xxxx;
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 23, 2020 3:10 PM