当从iOS 13.1中的SceneDelegate设置RootViewController时,UIElements变得不活动

时间:2019-10-23 16:54:08

标签: ios swift xamarin.ios ios13

我目前在Xamarin中遇到这些问题。iOS更新到iOS 13.1。 当我手动将LoginController设置为Main.Storyboard的初始ViewController时,一切正常,但是当我将LoginController设置为SceneDelegate.cs中的rootViewController时,当我的应用加载时,所有UIElement都将处于非活动状态。

这里是我的密码

    [Export("scene:willConnectToSession:options:")]
    public void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
    {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).

        //// Decide first screen
        UIWindowScene windowScene = new UIWindowScene(session, connectionOptions);

        var window = new UIWindow(windowScene);
        var storyboard = UIStoryboard.FromName("Main", null);

        var registerController = storyboard.InstantiateViewController("LoginViewController") as LoginViewController;
        this.SetWindow(window);


        window.RootViewController = registerController;
        window?.MakeKeyAndVisible();
        window.MakeKeyWindow();

    }

2 个答案:

答案 0 :(得分:1)

将视图控制器添加为所有内容的根控制器将使您的应用无法导航。视图控制器用于管理视图的控制器,除非您已经开发了视图控制器来像导航控制器一样管理视图,并且从它的外观来看,这不是您要做的。导航控制器管理视图控制器。因此,将根作为导航控制器,并将该导航控制器的根作为“ LoginViewController”,然后看看会发生什么。另外,停止使用故事板,如果/当您的应用达到100个以上的视图控制器时,您将遭受很大的痛苦。学习以编程方式处理该问题,您将很快了解整个导航过程如何工作以及如何与控制器和视图进行交互。

答案 1 :(得分:1)

原因是未将自定义窗口分配给this.Window。修改代码如下:

[Export("scene:willConnectToSession:options:")]
public void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).

    //// Decide first screen
    UIWindowScene windowScene = new UIWindowScene(session, connectionOptions);

    //var window = new UIWindow(windowScene);
    this.Window = new UIWindow(windowScene); 
    var storyboard = UIStoryboard.FromName("Main", null);

    var registerController = storyboard.InstantiateViewController("LoginViewController") as LoginViewController;
    //this.SetWindow(window);


    this.Window.RootViewController = registerController;
    this.Window?.MakeKeyAndVisible();
    this.Window.MakeKeyWindow();

}

然后它可以工作。

=================================更新============= ======================

如果不起作用,有两种查找原因的方法:

一种方法是确保跟随代码registerController不为null。

var registerController = storyboard.InstantiateViewController("LoginViewController") as LoginViewController;

您需要检查 StoryBoard 中的 StoryBoard ID 是否与 LoginViewController 匹配。 enter image description here

另一种方法,您可以创建一个不在StoryBoard内部的UIViewController类来进行检查。 enter image description here

 this.Window.RootViewController = new NewCreatedViewController();