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

时间:2017-05-23 19:11:53

标签: c# ios xamarin.ios uinavigationcontroller rootviewcontroller

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

Storyboard

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

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

1 个答案:

答案 0 :(得分:2)

问题是您正在创建一个新的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