我是 iOS 新手。我正在使用 Xamarin 的以下组件创建 SlideoutNavigation 菜单。 Github 链接如下。
链接: https://github.com/thedillonb/MonoTouch.SlideoutNavigation
在这个组件中,一切正常。但我有一个小问题。
最初我的 LoginViewController
看起来像这样。
截图:
左侧没有菜单按钮。
现在当我用菜单登录新打开的屏幕时是这样的
截图:
现在当打开 SlideOut 菜单并选择 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");
但是现在我在不想要的屏幕下方。我想删除导航中的左侧菜单图标。
截图:
感谢任何帮助。
更新:
打开新 Controller 的推送代码:
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();
}
我的流程:
我的项目只使用了一个 StoryBoard。所以所有的 ViewController 都在同一个 StoryBoard 中。
部署信息:
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(我的登录 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;
}
}
}
}
}
更新最新:
如果我在 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);
}
最佳答案
您在注销时将 SignInView Controller 插入堆栈——这是不正确的。以使用先前从堆栈中推送的 View 的方式编写代码。
在 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 ();
}
}
注销时..调用以下:
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);
}
}
}
关于ios - iOS (Xamarin) 中 MonoTouch.SlideoutNavigation 中的注销问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561274/