2

I need to have control of this method so that I can make a change in my app. But I couldn't make this implementation work, can anyone help?

Here is the Custom Renderer of my TabbedPage:

public class MainTabbedPageRenderer : TabbedRenderer, IUITabBarControllerDelegate
{
     [Export("tabBarController:shouldSelectViewController:")]
     public bool ShouldSelectViewController(UITabBarController tabBarController, UIViewController viewController)
     {
          return false;
     }
}

The breakpoint does not stop there at all.

I have the impression that it does not stop at breakpoint because TabBarController is always null, but the screen loads and performs navigations normally, I also could not make this TabBarController be filled.

You can click on tabbar items using this method:

[Export("tabBar:didSelectItem:")]
public void ItemSelected(UITabBar tabbar, UITabBarItem item)
{
}
Luiz Negrini
  • 656
  • 10
  • 32

1 Answers1

3

I don't see where you are assigning your delegate. That is likely why it is not hit, you have not assigned the delegate to the UITabBarController (which is the base class for TabbedRenderer). Also TabbedRenderer already assigns a delegate, so you likely do not want to replace it.

That said, Xamarin.iOS actually defines a C# delegate, called UITabBarSelection, for the ShouldSelectViewController protocol method. And there is a property on TabbedRenderer called ShouldSelectViewController that allows you to set this delegate method, so you should be able to just do this:

public class MainTabbedPageRenderer : TabbedRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
          this.ShouldSelectViewController = ShouldSelectViewControllerHandler;
    }

    bool ShouldSelectViewControllerHandler(UITabBarController tabBarController, UIViewController viewController)
    {
        return false;
    }
}
jgoldberger - MSFT
  • 5,978
  • 2
  • 20
  • 44
  • ShouldSelectViewController is a method, why can't you put a value as you answered in the answer, something could be implemented wrong, can you supplement your answer with more pieces of code? – Luiz Negrini Oct 22 '19 at 00:55
  • There is a property on the `TabbedRenderer` type called `ShouldSelectViewController`, and the type is `UITabBarSelection` which is a C# delegate. So you can assign a method with the same signature as the C# delegate to that property. I tested the above and it works as expected. – jgoldberger - MSFT Oct 22 '19 at 00:55
  • please, put in your response – Luiz Negrini Oct 22 '19 at 00:57
  • My answer is complete and works as written. Perhaps you are getting confused between Obj-C delegates and C# delegates. IN Obj-C, a delegate is usually a protocol (similar to a C# interface) and is an object used to handle events from the object that uses that protocol (Obj-C delegate), whereas a C# delegate is just a definition of a method signature and can be used as the type for a C# property such that you can assign a method to a property, as above. Read up on C# delegates if you need more info. – jgoldberger - MSFT Oct 22 '19 at 01:01
  • I need a solution that runs every time I click the tabbar buttons, is it possible? – Luiz Negrini Oct 22 '19 at 01:01
  • Yes, see the above answer. I tested this. That method runs every time I click the tab bar, and returning false does make it so the new tab is not selected, or rather the view controller is NOT shown. – jgoldberger - MSFT Oct 22 '19 at 01:02
  • If it is not working for you, explain what is happening. – jgoldberger - MSFT Oct 22 '19 at 01:02
  • Ok, I'm testing! – Luiz Negrini Oct 22 '19 at 01:03
  • Oh, you need to remove the `IUITabBarControllerDelegate` inheritance as that defines a method called `ShouldSelectViewController` which conflicts with the property name, so it confuses things. That may be why you said that `ShouldSelectViewController` is a method and can't be assigned. But you see my answer did remove that from the beginning. – jgoldberger - MSFT Oct 22 '19 at 01:10
  • oh yeah! Thanks man, works perfect! hehe I'll put your response on Xamarin forum, can you put in there? – Luiz Negrini Oct 22 '19 at 01:13
  • 2
    Oh, I see that: https://forums.xamarin.com/discussion/145051/cancel-itemselected-event-in-tabbarrenderer-xamarin-ios#latest . ColeX is dong the same thing, just using a lambda rather than a named method. The end result is exactly the same though. – jgoldberger - MSFT Oct 22 '19 at 01:16