【问题标题】:How to connect multiple UIViewControllers without StoryBoard (pure code)? Xamarin.iOS native如何在没有 StoryBoard(纯代码)的情况下连接多个 UIViewController? Xamarin.iOS 本机
【发布时间】:2019-10-11 04:07:52
【问题描述】:

我有多个 UIViewController,但我无法通过按钮连接它们。其中之一必须是 UINavigationController,因为我需要“后退”按钮。这个视图是否更容易成为 UIViewController 并且我手动添加一个按钮来“返回”?

我没有使用storyboard/swift/objective-c,它是xamarin.ios native

【问题讨论】:

  • 谢谢。我发誓我已经搜索过,但没有找到任何有用的帖子。我应该删除问题吗?
  • 我这样做时的一个问题:this.PresentViewController(new UIViewControllerSecond(), true, null);另一个视图在背景中。无论如何,这个视图是否会消失并且当前的 UIViewController 完全负责?
  • 后台视图不会消失。
  • 唯一的办法就是将该视图设置为 RootController 对吗?

标签: c# xamarin xamarin.ios


【解决方案1】:

memyselfandi,有很多不同的概念要理解,你可以下一个断点来查看你的 rootViewController 的结构。为了在您的应用中的任何位置获取 RootViewController,您可以这样做:

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

如果您的 RootViewController 没有 UINavigationController,则有两种添加 ViewController 的方法:

  1. 您只能从 RootViewController 内部通过 PresentViewController 显示您的 UIViewController,因此它显示为模态,而其他视图可以在后台看到。正如您已经知道的那样,这只是像这样完成
PresentViewController(vc, true, null);
  1. 或者您可以用包含新 UIViewController 的 UINavigationController 替换 RootViewController。然后继续将内容推送到 NavigationController 堆栈。这将防止其他视图留在后台。
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;

奖励:当您将 viewControllers 以奇怪的方式堆叠在其他 viewControllers 之上时,它会变得有点复杂,因此您可以通过以下方式传递您的 viewController:

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);
}

【讨论】:

  • 很高兴您能理解。我试图尽可能清楚地说明这一点。随时提问。
  • 是的!你说的很清楚!太感谢了!!你回答的正是我需要知道的!再次感谢你hh!如果您不介意,我还有另一个问题:如果我使用 UINavigationController 而不是使用 ViewControllers,我可以创建 UIViews 例如对吗?然后推?我将在哪里创建这个 NavigationController?在 AppDelegate 中?
  • 没问题。 UIViews 不能直接添加,它们必须添加到 UIViewController。 UIViews 就像需要添加到 Pages/ViewControllers 中的控件/视图/组件。 UINavigationController 就像 UIViewControllers 的容器
猜你喜欢
  • 2013-11-07
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多