如何以编程方式设置视图控制器

时间:2016-06-11 16:54:21

标签: c# ios xamarin.ios

我正在尝试使用iOS 7/8风格的模糊视图控制器,它出现在你的视图控制器顶部的xamarin与故事板界面,我把它连接到我的导航控制器确定,但我想要实现的是用户必须首先登录,所以理论上菜单不应该首先显示,但我想要我的登录故事板文件。

哪个xamarin基于此https://github.com/romaonthego/REFrostedViewController

构建了一个尖锐的库
// Override FinishedLaunching. This executes after the app has started.

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
      this.Window = new UIWindow(UIScreen.MainScreen.Bounds);

        // ViewControllers
        var aRootVC = new DEMOHomeViewController();
        var secondVC = new  loginViewController();
        var loginViewControl = new loginViewController();

        //define the menu structure
        var sections = new List<REMenuItemSection>()
        {

            new REMenuItemSection()
            {
                Items = new List<REMenuItem>()
                {
                    new REMenuViewControllerItem()
                    {
                        //View exisiting view controller, will be reused everytime the item is selected
                        Icon = UIImage.FromBundle(@"home-48"),
                        Title = @"Home",
                        ViewController = loginViewControl,
                    },
                    new REMenuViewControllerItem()
                    {
                        //New view controller, will be reused everytime the item is selected
                        Icon = UIImage.FromBundle(@"about-48"),
                        Title = @"Payments Missed",
                        ViewController = secondVC,
                    },
                    new REMenuViewControllerItem()
                    {
                        //New view controller, will be reused everytime the item is selected
                        Icon = UIImage.FromBundle(@"about-48"),
                        Title = @"Payments Made",
                        ViewController = secondVC,
                    },  new REMenuViewControllerItem()
                    {
                        //New view controller, will be reused everytime the item is selected
                        Icon = UIImage.FromBundle(@"about-48"),
                        Title = @"Upload Doucments",
                        ViewController = secondVC,
                    },

                        new REMenuViewControllerItem()
                    {
                        //New view controller, will be reused everytime the item is selected
                        Icon = UIImage.FromBundle(@"about-48"),
                        Title = @"Update Info",
                        ViewController = secondVC,
                    },

                        new REMenuViewControllerItem()
                    {
                        //New view controller, will be reused everytime the item is selected
                        Icon = UIImage.FromBundle(@"about-48"),
                        Title = @"Amend I & E",
                        ViewController = secondVC,
                    }
                },
            },
            new REMenuItemSection()
            {
                Title = "Friends Online",
                Items = new List<REMenuItem>()
                { 
                    new REMenuActionItem()
                    {
                        //Action is called, on the UI thread, everytime the item is selected
                        Icon = UIImage.FromBundle(@"ask_question-48"),
                        Title = @"Logout",
                        Command = ()=>
                        {
                            var uiAlert = new UIAlertView("Logout","Are you sure you want to log out?",null,"No","Yes");
                            uiAlert.Show();
                        },
                    },
                },
            },
        };

但当我将其切换到我的登录视图时,它会显示以下内容

  

无法将类型隐式转换为ui kit视图控制器,因为它需要ui视图而不能查看,所以如何在显示菜单之前显示我的登录屏幕

enter image description here

enter image description here

我的登录视图类

public partial class loginViewController : UIView
{
    const string serviceId = "ApertureBel";
    NSError error;
    LAContext context = new LAContext();

    partial void touchButton(UIButton sender)
    {
        //Lets double check the device supports Touch I
        if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out error))
        {
            var replyHandler = new LAContextReplyHandler((success, error) =>
                {
                    InvokeOnMainThread(() =>
                        {
                            if (success)
                            {
                //              var homeScreen = (UIViewController)Storyboard.InstantiateViewController("welcome");
                //              PresentViewController(homeScreen, true, () => { });
                            }
                            else
                            {
                                var alert = new UIAlertView("Finger Print!", "Sorry Finger Print is not on file.", null, "Aperturer", null);
                                alert.Show();
                            }
                        });
                });
            context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "Logging in with Touch ID", replyHandler);
        }
        else
        {
            var alert = new UIAlertView("Error", "TouchID not available", null, "Aperture", null);
            alert.Show();
        }
    }


    bool CheckLogin(string username, string password)
    {
        if (password == Helpers.KeychainHelpers.GetPasswordForUsername(username, serviceId, true) && username == NSUserDefaults.StandardUserDefaults.ValueForKey(new NSString("username")).ToString())
            return true;
        else
        {
            return false;
        }
    }


    //  partial void buttonLoginClick(UIButton sender)
    //{
    //  if (string.IsNullOrEmpty(txtUserName.Text) | string.IsNullOrEmpty(txtPassword.Text))
    //  {
    //      var alert = new UIAlertView("Oops!", "You must enter both a username and password", null, "Oops", null);
    //        alert.Show();
    //      return;
    //  }

    //  if (CheckLogin(txtUserName.Text, txtPassword.Text))
    //  {
    //      var homeScreen = (UIViewController)Storyboard.InstantiateViewController("welcome");
    //      PresentViewController(homeScreen, true, () => { });
    //  }
    //  else
    //  {
    //      var alert = new UIAlertView("Login problem", "wrong username", null, "Oops...again", null);
    //      alert.Show();
    //  }
    //}
}
}

1 个答案:

答案 0 :(得分:0)

我的代码与你的代码相同。 就我而言,我在登录前隐藏了导航控制器。

首次加载页面时,在LoginViewController中使用此代码 (如果用户没有登录则隐藏。)

this.NavigationController.SetNavigationBarHidden(true, true);//hide nav

登录成功后,显示导航控制器!

this.NavigationController.SetNavigationBarHidden(false, true);//show nav 这样您就不需要在菜单中更改代码了〜