Thursday, January 26, 2017

Logout Issue in MonoTouch.SlideoutNavigation in iOS(Xamarin)

Leave a Comment

I am newbie in iOS. I am creating SlideoutNavigation Menu using below Component of Xamarin. The Github link is below.

Link : https://github.com/thedillonb/MonoTouch.SlideoutNavigation

In this Component the whole thing is working fine. But I have a little Issue.

Initially my LoginViewController is looking like this.

Screenshot :

enter image description here

There is no Menu Button in the Left side.

Now When I login the New Open Screen with the Menu is look like this

Screenshot :

enter image description here

Now When open the SlideOut menu and select the Logout Option I want to start my LoginViewController it also working fine using below code.

Code :

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

But Now I am getting below Screen which is not want. I want to remove that Left Menu icon in the Navigation.

Screenshot :

enter image description here

Any Help be Appreciated.

Update :

push code for new Controller open :

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();                     } 

My Flow :

I have use only one StoryBoard for my Project. So all the ViewController are in the same StoryBoard.

Deployment Info :

enter image description here

AppDelegate.cs I am not changing anything in this file.

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 (is My Login view Controller)

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;                 }             }         }      } } 

Update latest :

If I do this in the DummyLeftControllers.cs then nothing is happen :

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

1 Answers

Answers 1

You are pushing SignInView controller on stack on Logout--which is incorrect. Write your code in a way that It use previously pushed view from stack..

In Splashviewconrtroller change following in viewdidload method:

 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 ();                  }               } 

On Logout..Call following:

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);             }         }          } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment