如何打开 NavigationController root 并传递一个值?

How to open NavigationController's root and pass a value?

我正在创建 Xamarin.iOS 应用程序的登录部分。登录时,我想将一个用户对象传递给 ProjectsViewController。首先用户输入用户名和密码,然后进行验证,最后,一旦通过验证,我想转到名为 ProjectsViewController 的主屏幕。

//first get user from Restful service 
var response = await apiService.GetUserByToken("http://192.168.1.20:55405",
            "/api",
            "/Account/FullUser",
            token.TokenType,
            token.AccessToken);
        //get User object
        var user = (User)response.Result;
        user.Password = password;
        if (response.IsSuccess)
        {
            //loguser is the User object that will be passed to the root
            logUser = user;
        }            

        //get storyboard 
        UIStoryboard board = UIStoryboard.FromName("Main", null);
        ProjectsViewController ctrl = (ProjectsViewController)board.InstantiateViewController("projectsViewController");
        //current user is the propery that I want to access in the next screen
        ctrl.CurrentUser = logUser;
        ctrl.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical;

        UINavigationController nav = (UINavigationController)board.InstantiateViewController("navController");
        nav.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
        //this sends me to the root but the object is not passed
        PresentViewController(nav, true, null);

到目前为止,代码验证了用户并将我发送到根屏幕,但未传递对象。请帮忙! 提前致谢!

问题是您正在创建以下的新实例:ProjectsViewController 并设置其 CurrentUser,但您根本没有显示该控制器。

你应该做的是:

UINavigationController nav = (UINavigationController)board.InstantiateViewController("navController");
nav.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
//New code
var projectVC = (ProjectsViewController)nav.ViewControllers.FirstOrDefault();
projectVC.CurrentUser = logUser;

PresentViewController(nav, true, null);

因为当您从情节提要中实例化:"navController" 它也会实例化它的 ProjectsViewController,它在它的 ViewControllers 列表中。