Keith
Keith

Reputation: 5391

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.

Upvotes: 0

Views: 320

Answers (2)

Keith
Keith

Reputation: 5391

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.

Upvotes: 0

Junior Jiang
Junior Jiang

Reputation: 12721

Generally , we can use PresentViewController to navigate to another page in Navigation Controller , this should be a safe and easy way.

Push :

enter image description here

Pop :

enter image description here

However , PresentModalViewController is a model ViewController , although it seems like the same as PresentViewController . It not needs to be in the stack of Navigation Controller ,and always used in a single view needs to show temporarily , soon will dismiss it.

If scene needs to reset RootViewControllerwhen runtime , this can be thought as the third way to navigate .

var storyboard = UIStoryboard.FromName("Main", null);
var anotherController = storyboard.InstantiateViewController("AnotherViewController") as AnotherViewController;
UIWindow window =  UIApplication.SharedApplication.KeyWindow;
if(window != null)
{
    window.RootViewController = anotherController;
}

In addittion , this will be un-safe way to use , better used them in current ViewController dismiss method :

DismissViewController(true, () => {
      // Set Root View Controller
});

Upvotes: 1

Related Questions