简体   繁体   中英

Xamarin.ios login page, cannot change the first page of storyboard

I am creating an application using xamarin.ios. I have some pages in my storyboard and I have set "Is initial View Controller" for one of the pages as the first page. Users see that page as the first page if they already logged in otherwise I want to show the login page. To do that I added this code:

        [Export ("application:didFinishLaunchingWithOptions:")]
        public bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
        {
            this.Window = new UIWindow(UIScreen.MainScreen.Bounds);
            LoginViewController yourViewController = UIStoryboard.FromName("Main", NSBundle.MainBundle).InstantiateViewController("LoginViewController") as LoginViewController;

            this.Window.RootViewController = yourViewController;
            this.Window.MakeKeyAndVisible();
            return true;
        }

The problem is, this code cannot change the first page at all. It seems my application always ignore this code and shows the page with "Is Initial View controller" equals true on storyboard. It means when I change first page on storyboard I see the change but no change when I set it in my code.

The app delegate's role changes from iOS 12 to iOS 13, the SceneDelegate is responsible for setting up the scenes of your app :

[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).

    UIWindowScene myScene = scene as UIWindowScene;
    Window = new UIWindow(myScene);
    UIViewController viewController = new UIViewController();
    UINavigationController navigationMain = new UINavigationController(viewController );
    Window.RootViewController = navigationMain;
    Window.MakeKeyAndVisible();
}

Here are some threads about this question in swift: set-rootviewcontroller-ios-13 and ios-13-swift-set-application-root-view

And articles: accessing-root-view-controller-ios13-scenedelegate and scene-delegate-app-delegate-xcode-11-ios-13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM