Reputation: 71
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
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:
PresentViewController(vc, true, null);
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