memyselfandi
memyselfandi

Reputation: 71

How to connect multiple UIViewControllers without StoryBoard (pure code)? Xamarin.iOS native

I have multiple UIViewControllers, but I can't connect them through buttons. One of them must be an UINavigationController because I need the "back" button. Is it easier for this view being an UIViewController and I add a button manually to "go back"?

I am not using storyboard/swift/objective-c, it's xamarin.ios native

Upvotes: 1

Views: 182

Answers (1)

Saamer
Saamer

Reputation: 5109

memyselfandi, There's a lot of different concepts to understand, and you can place a breakpoint to see the structure of your rootViewController. In order to get the RootViewController anywhere in your app, you can do this:

var appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
var rootVC = appDelegate.Window.RootViewController;

There's two way of adding a ViewController if your RootViewController does not have a UINavigationController:

  1. You can only show your UIViewController through a PresentViewController from inside that RootViewController, so it appears as a modal and the other view can be seen in the background. This, as you already know, is simply done like this
PresentViewController(vc, true, null);
  1. Or you can replace that RootViewController with a UINavigationController that contains your new UIViewController. And then just keep pushing things on to the NavigationController stack. This will prevent the other views to stay in the background.
var navController = new UINavigationController(vc); 
// where, vc is the ViewController you want to replace the existing one with.
// Eg: think of situations where you login a user.
rootVC = navController;

Bonus: It gets a little more complicated when you have viewControllers on top of other viewControllers stacked up in weird ways, so you can pass your viewController through something like this:

public static void Push(UIViewController vc)
{
    // to get the RootViewController, we have to get it from the AppDelegate
    var appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
    var rootVC = appDelegate.Window.RootViewController;

    // If you want to push to a ModalViewController which consists of a NavigationController
    if (rootVC.PresentedViewController != null && rootVC.PresentedViewController.NavigationController != null)
        rootVC.PresentedViewController.NavigationController.PushViewController(vc, true);

    // If there already is a NavigationController, you can do a simple push
    else if (rootVC.NavigationController != null)
        rootVC.NavigationController.PushViewController(vc, true);

    // If the NavigationController exists in a TabBar, we have to push on that
    else if (rootVC != null
        && rootVC is UITabBarController tabbarController
        && tabbarController.SelectedViewController is UINavigationController navigationController)
        navigationController.PushViewController(vc, true);

    // If all else fails, present the ViewController as a modal
    else if (rootVC != null)
        rootVC.PresentViewController(vc, true, null);
}

Upvotes: 1

Related Questions