2017-01-10 37 views
3

我是iOS的新手。我正在使用下面的Xamarin组件创建SlideoutNavigation Menu。 Github链接如下。iOS中的MonoTouch.SlideoutNavigation注销问题(Xamarin)

链接:https://github.com/thedillonb/MonoTouch.SlideoutNavigation

这个组件中的整个事情是工作的罚款。但我有一个小问题。

最初我的LoginViewController看起来像这样。

截图:

enter image description here

有一个在左侧无菜单按钮。

现在,当我登录新开放屏幕与菜单是这个样子

截图:

enter image description here

现在,当打开时滑出式菜单,然后选择Logout选项我想开始我的LoginViewController它也使用下面的代码工作正常。

代码:

var loginViewController = storyboard.InstantiateViewController("ViewController") as ViewController; 
BizApplication.clearCredential(); 
StyledStringElement logout = new StyledStringElement("Logout",() => NavigationController.PushViewController(loginViewController, true)){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
logout.Image = new UIImage("filter_icon.png"); 

,但现在我越来越下方未想屏幕。我想要移除导航中的左侧菜单图标。

截图:

enter image description here

任何帮助理解。

更新:

新的控制器开启推代码:

if (BizApplication.getCredential() != null) 
        { 
         window = new UIWindow(UIScreen.MainScreen.Bounds); 
         Menu = new SlideoutNavigationController(); 

         var webController2 = Storyboard.InstantiateViewController("SearchViewController") as SearchViewController; 
         NavigationController.PushViewController(webController2, true); 

         Menu.MainViewController = new MainNavigationController(webController2, Menu); 
         Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = true }; 

         window.RootViewController = Menu; 
         window.MakeKeyAndVisible(); 

         loadingOverlay.Hide(); 
        } 

我的流程:

我只用一个我的项目故事板。所以所有的ViewController都在同一个StoryBoard中。

部署信息:

enter image description here

AppDelegate.cs我不会改变这个文件什么。

SplashViewController。CS

public partial class SplashViewController : UIViewController 
    { 
     UIWindow window; 

     UIViewController container; 

     UIStoryboard storyboard; 

     public SlideoutNavigationController Menu { get; private set; } 

     public SplashViewController(IntPtr handle) : base(handle) 
     { 

     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
      if (Reachability.IsHostReachable("http://google.com")) 
      { 
       if (BizApplication.CheckCredential()) 
       { 
        window = new UIWindow(UIScreen.MainScreen.Bounds); 

        Menu = new SlideoutNavigationController(); 

        storyboard = UIStoryboard.FromName("Main", null); 
        var webController = storyboard.InstantiateViewController("SearchViewController") as SearchViewController; 

        NavigationController.PushViewController(webController, true); 

        Menu.MainViewController = new MainNavigationController(webController, Menu); 
        Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = true }; 

        window.RootViewController = Menu; 
        window.MakeKeyAndVisible(); 

       } 
       else { 
        storyboard = UIStoryboard.FromName("Main", null); 
        var webController = storyboard.InstantiateViewController("ViewController") as ViewController; 
        this.NavigationController.PushViewController(webController, true); 

       } 
      } 
     } 

     public void pushMenu() 
     { 

      UINavigationController navMin = (UINavigationController)window.RootViewController; 
      Menu = new SlideoutNavigationController(); 
      storyboard = UIStoryboard.FromName("Main", null); 
      var webController = storyboard.InstantiateViewController("SearchViewController") as SearchViewController; 

      Menu.MainViewController = new MainNavigationController(webController, Menu); 
      Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = true }; 
      navMin.PushViewController(Menu, true); 

     } 


} 

ViewController.cs(是我的登录视图控制器)

public partial class ViewController : UIViewController 
    { 
     LoadingOverlay loadingOverlay; 

     UIWindow window; 

     public SlideoutNavigationController Menu { get; private set; } 

     protected ViewController(IntPtr handle) : base(handle) 
     { 
     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      this.Title = "Log In"; 
      UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent; 


     } 

     public override void ViewDidAppear(bool animated) 
     { 
      base.ViewDidAppear(animated); 

      this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes() { ForegroundColor = UIColor.White }; 
      this.NavigationItem.SetHidesBackButton(true, false); 
      this.NavigationController.NavigationBar.BarTintColor = UIColor.Clear.FromHexString("#0072BA", 1.0f); 
      this.NavigationController.NavigationBarHidden = false; 

      txtfield_Username.Layer.BorderWidth = 1.0f; 
      txtfield_Username.Layer.BorderColor = UIColor.Clear.FromHexString("#000000", 1.0f).CGColor; 

      txtfield_password.Layer.BorderWidth = 1.0f; 
      txtfield_password.Layer.BorderColor = UIColor.Clear.FromHexString("#000000", 1.0f).CGColor; 

      lbl_forgetPassword.TextColor = UIColor.Clear.FromHexString("#0072BA", 1.0f); 

      btn_register.TouchUpInside += (sender, e) => 
      { 
       var webController = Storyboard.InstantiateViewController("RegisterController") as RegisterController; 
       NavigationController.PushViewController(webController, true); 
      }; 

      btn_login.TouchUpInside += async (sender, e) => 
      { 
       loadingOverlay = new LoadingOverlay(UIScreen.MainScreen.Bounds); 
       View.Add(loadingOverlay); 

       Token token = await Authonicator.Authonicate(txtfield_Username.Text, txtfield_password.Text); 
       if (token != null) 
       { 
        AppCredentials credentials = new AppCredentials(); 
        credentials.Token = token; 
        credentials.UserName = txtfield_Username.Text; 

        var userItem = await UserClient.GetUserInfo(token.Access_token); 
        if (userItem != null) 
        { 
         credentials.Id = userItem.Id; 
         credentials.Name = userItem.Name.FirstName + " " + userItem.Name.LastName; 
         credentials.Names.FirstName = userItem.Name.FirstName; 
         credentials.Names.MiddleName = userItem.Name.MiddleName; 
         credentials.Names.LastName = userItem.Name.LastName; 
         credentials.Role = userItem.Role; 
         credentials.Contact = userItem.Mobile; 
        } 
        BizApplication.setCredential(credentials); 

        if (BizApplication.getCredential() != null) 
        { 
         window = new UIWindow(UIScreen.MainScreen.Bounds); 
         Menu = new SlideoutNavigationController(); 

         var webController2 = Storyboard.InstantiateViewController("SearchViewController") as SearchViewController; 

         Menu.MainViewController = new MainNavigationController(webController2, Menu); 
         Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = true }; 

         window.RootViewController = Menu; 
         window.MakeKeyAndVisible(); 

         loadingOverlay.Hide(); 
        } 
       } 
       else { 

        UIAlertController alert = UIAlertController.Create("Authorization", "Enter Valid Username and Password", UIAlertControllerStyle.Alert); 
        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, Action => { })); 
        PresentViewController(alert, true, null); 

        loadingOverlay.Hide(); 
       } 
      }; 
     } 

     public override void DidReceiveMemoryWarning() 
     { 
      base.DidReceiveMemoryWarning(); 
     } 
    } 

DummyControllerLeft.cs

using System; 
using System.Drawing; 
using System.Threading.Tasks; 
using CoreAnimation; 
using Foundation; 
using Gargi.Business; 
using MonoTouch.Dialog; 
using UIKit; 

namespace Gargi.iOS 
{ 
    public class DummyControllerLeft : DialogViewController 
    { 
     public static UIImageView profileImage; 

     public DummyControllerLeft(IntPtr handle) : base(handle) 
     { 

     } 

     public DummyControllerLeft() 
      : base(UITableViewStyle.Plain, new RootElement("")) 
     { 

      var storyboard = UIStoryboard.FromName("Main", null); 

      var webController = storyboard.InstantiateViewController("SearchViewController") as SearchViewController; 
      StyledStringElement search = new StyledStringElement("Search",() => NavigationController.PushViewController(webController, true)) { TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      search.Image = new UIImage("filter_icon.png"); 

      var appointController = storyboard.InstantiateViewController("AppointmentListController") as AppointmentListController; 
      StyledStringElement appointment = new StyledStringElement("Appointment",() => NavigationController.PushViewController(appointController, true)) { TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      appointment.Image = new UIImage("filter_icon.png"); 

      var caseHistoryController = storyboard.InstantiateViewController("CaseHistoryController") as CaseHistoryController; 
      StyledStringElement casehistory = new StyledStringElement("CaseHistory",() => NavigationController.PushViewController(caseHistoryController, true)){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      casehistory.Image = new UIImage("filter_icon.png"); 

      var accountController = storyboard.InstantiateViewController("AccountViewController") as AccountViewController; 
      StyledStringElement account = new StyledStringElement("Account",() => NavigationController.PushViewController(accountController, true)){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      account.Image = new UIImage("filter_icon.png"); 

      var securityController = storyboard.InstantiateViewController("SecurityViewController") as SecurityViewController; 
      StyledStringElement security = new StyledStringElement("Security",() => NavigationController.PushViewController(securityController, true)){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      security.Image = new UIImage("filter_icon.png"); 

      var workProfileController = storyboard.InstantiateViewController("WorkProfileViewController") as WorkProfileViewController; 
      StyledStringElement workProfile = new StyledStringElement("WorkProfile",() => NavigationController.PushViewController(workProfileController, true)){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      workProfile.Image = new UIImage("filter_icon.png"); 

      BizApplication.clearCredential(); 

      StyledStringElement logout = new StyledStringElement("Logout",() => CallthisMethod()){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      logout.Image = new UIImage("filter_icon.png"); 

      Root.Add(new Section() 
      { 
       search, 
       appointment, 
       casehistory, 
       account, 
       security, 
       workProfile, 
       logout 
      }); 

      TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None; 
      TableView.BackgroundColor = UIColor.Clear.FromHexString("#0072BA", 1.0f); 
     } 

     void CallthisMethod() 
     { 
      var vwControllers = NavigationController.ViewControllers; 

      foreach (UIViewController signiinVC in vwControllers) 
      { 
       if (signiinVC.GetType() == typeof(ViewController)) 
       { 
        this.NavigationController.PopToViewController(signiinVC,true); 
       } 
      } 
     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      TableView.Frame = new RectangleF((float)TableView.Frame.Left, (float)(TableView.Frame.Top + 30), (float)TableView.Frame.Width, (float)(TableView.Frame.Height - 30)); 
      UIView headerView = new UIView(); 
      headerView.Frame = new CoreGraphics.CGRect(0, 0, TableView.Frame.Width, 140); 
      //headerView.BackgroundColor = UIColor.Clear.FromHexString("#004F80", 1.0f); 

      profileImage = new UIImageView(); 
      profileImage.Frame = new CoreGraphics.CGRect(10, 10, 70, 70); 
      profileImage.Layer.CornerRadius = 35; 
      profileImage.ClipsToBounds = true; 
      profileImage.Image = UIImage.FromBundle("gargi_logo.png"); 

      UILabel userName = new UILabel(); 
      userName.Frame = new CoreGraphics.CGRect(10, 90, TableView.Frame.Width - 20, 20); 
      userName.Font = UIFont.FromName("Helvetica-Bold", 14f); 
      userName.TextColor = UIColor.White; 
      headerView.AddSubview(userName); 

      UILabel userRole = new UILabel(); 
      userRole.Frame = new CoreGraphics.CGRect(10, 110, TableView.Frame.Width - 20, 20); 
      userRole.Font = UIFont.FromName("Helvetica-Bold", 14f); 
      userRole.TextColor = UIColor.White; 
      headerView.AddSubview(userRole); 

      headerView.AddSubview(profileImage); 
      TableView.TableHeaderView = headerView; 

      if (BizApplication.getCredential().Name != null) 
      { 
       userName.Text = BizApplication.getCredential().Name; 
      } 

      if (BizApplication.getCredential().Role != null) 
      { 
       userRole.Text = BizApplication.getCredential().Role; 
      } 

      var gradient = new CAGradientLayer(); 
      gradient.Frame = headerView.Frame; 
      gradient.Colors = new CoreGraphics.CGColor[] { UIColor.Clear.FromHexString("#0072BA", 1.0f).CGColor,UIColor.Clear.FromHexString("#004f80",1.0f).CGColor}; 
      headerView.Layer.InsertSublayer(gradient, 0); 

      var task = GetUserImage(); 

     } 

     private async Task GetUserImage() 
     { 
      var userHeader = await UserClient.GetHeaderData(); 
      if (!string.IsNullOrEmpty(userHeader.Image)) 
      { 
       string trimbase = userHeader.Image.Trim('"'); 
       try 
       { 
        var imageBytes = Convert.FromBase64String(trimbase); 
        var imageData = NSData.FromArray(imageBytes); 
        profileImage.BackgroundColor = UIColor.White; 
        profileImage.Layer.CornerRadius = 35; 
        profileImage.ClipsToBounds = true; 
        profileImage.Image = UIImage.LoadFromData(imageData); 
       } 
       catch (Exception ex) 
       { 
        string msg = ex.Message; 
       } 
      } 
     } 

    } 
} 

更新最新:

如果我这样做的DummyLeftControllers.cs那么没有什么是发生:

StyledStringElement logout = new StyledStringElement("Logout",() => CallthisMethod(storyboard)){ TextColor = UIColor.White, BackgroundColor = UIColor.Clear }; 
      logout.Image = new UIImage("filter_icon.png"); 


void CallthisMethod(UIStoryboard storyboard) 
     { 
      var vwControllers = NavigationController.ViewControllers; 

      foreach (UIViewController signiinVC in vwControllers) 
      { 
       if (signiinVC.GetType() == typeof(ViewController)) 
       { 
        this.NavigationController.PopToViewController(signiinVC, true); 
       } 
      } 

      BizApplication.clearCredential(); 

      NavigationController.PopToRootViewController(true); 

    } 
+0

你怎么推新屏幕完成登录时?你能分享代码吗? – Darshana

+0

@NinjaHattori查看我的更新代码。 – Ironman

+0

@NinjaHattori如果你需要其他代码,然后告诉我,我会更新它。 – Ironman

回答

1

你推退出堆栈SignInView控制器 - 这是不正确。编写代码的方式,它使用来自堆以前压看法..

在Splashviewconrtroller变化按照viewDidLoad方法:

base.ViewDidUnload(); 

      storyboard = UIStoryboard.FromName ("Main", null); 
      var webController = storyboard.InstantiateViewController ("ViewController") as ViewController; 
      this.NavigationController.PushViewController (webController, false); 


      if (Reachability.IsHostReachable ("http://google.com")) { 
       if (BizApplication.CheckCredential()) { 
        //window = new UIWindow (UIScreen.MainScreen.Bounds); 

        Menu = new SlideoutNavigationController(); 

        storyboard = UIStoryboard.FromName ("Main", null); 
        var webController = storyboard.InstantiateViewController ("SearchViewController") as SearchViewController; 

        NavigationController.PushViewController (webController, false); 

        Menu.MainViewController = new MainNavigationController (webController, Menu); 
        Menu.MenuViewController = new MenuNavigationController (new DummyControllerLeft(), Menu) { NavigationBarHidden = true }; 
        this.NavigationController.PushViewController (Menu); 
        //window.RootViewController = Menu; 
        //window.MakeKeyAndVisible(); 

       } 
      } 

在Logout..Call如下:

void onLogOut (object sender, EventArgs e) 
     { 
    //Write your code to clear 
      var vwControllers = this.NavigationController.ViewControllers; 
     foreach(UIViewController signinVC in vwControllers) { 
      if (signinVC.GetType() == typeof (ViewController)) { 
       this.NavigationController.PopToViewController (ViewController); 
      } 
     } 

     } 
+0

等待我尝试解决方案并尽快回复给您。 – Ironman

+0

我有liitle问题因为我在故事板中使用了'SpalshViewController'。 – Ironman

+0

你能解释一下你有什么问题吗? – Darshana

0

我认为你应该以模态方式呈现你的视图控制器,因为你不需要后退按钮,所以不需要将它们推入堆栈。

,而不是

navigationcontroller.pushviewcontroller() 

使用

presentviewcontroller() 

可以使用dismissviewcntroller()当注销,这将不会在登录屏幕改变你的导航栏