Search code examples
xamarinxamarin.iosuitabbarcontrolleruistoryboard

Xamarin iOS: Replace View Controller within the current Tab Bar item


I am just starting up in Xamarin iOS (native) development, so bear with me as I try to explain what we're trying to do.

I have a single storyboard Main.storyboard, where I have designed and built a single Tab Bar controller, and several custom View Controllers, some of which are linked up as tabs. On the first tab, I have a login screen if they are not logged in. However, if they ARE logged in, I want to show the view defined in a different View Controller (Account) that exists within the same storyboard.

In the ViewDidLoad() override for the LoginController, I have the following code that I thought would show the "logged in" Account View Controller:

this.PresentViewController(new AccountController(), true, null);

This line of code executes without error, however the Login view is still shown. The ViewDidLoad() override in the AccountController does NOT fire.

Question 1: Is this the right method to call in order to replace your current view controller with another?

Now, if I place this exact same line of code in an async button handler method (particularly, the Facebook SDK Login Button's Completed event), it actually hits the ViewDidLoad() override in the AccountController, but I receive a NullReferenceException on a line that is simply referencing a button that is defined within the storyboard/view.

Question 2: Why would the same exact call from an event handler behave so differently?

Lastly, if I set the AccountController up as a tab bar tab instead, the code in ViewDidLoad() that is referencing the button works as expected.

Up to this point, all of my loading and unloading of different views was being handled automagically by the UITabBarController, but now that I'm trying to do some manual transitions, I am having a tough time finding appropriate documentation that isn't outdated and applies to my specific setup.


Solution

  • While Junior's answer pointed me in the right direction, the ultimate solution was to adjust the first tab to be a new NavigationController, and use (manual) Segues to navigate between the views.

    The segues were added to the storyboard, and the View Controllers ultimately make calls to PerformSegue('namedSegue', this) to navigate to the next view.