Skip to content

Instantly share code, notes, and snippets.

@Alxandr
Created July 2, 2012 16:44
Show Gist options
  • Save Alxandr/3034182 to your computer and use it in GitHub Desktop.
Save Alxandr/3034182 to your computer and use it in GitHub Desktop.
Helper for displaystack in ios
using System;
using MonoTouch.UIKit;
using System.Collections.Generic;
using FdvWeb.Core;
namespace FdvWeb
{
public class DisplayStack
{
readonly UITabBarController root;
readonly Stack<UIViewController> modalStack;
public DisplayStack (UITabBarController root)
{
this.root = root;
this.modalStack = new Stack<UIViewController> ();
}
public void ShowModal (UIViewController viewController, bool animated = true)
{
Util.RunOnUIThread (delegate {
if (modalStack.Count == 0)
root.PresentModalViewController (viewController, animated);
else
modalStack.Peek ().PresentModalViewController (viewController, animated);
modalStack.Push (viewController);
});
}
public void DismissModal (bool animated = true)
{
modalStack.Pop ();
IoCContainer.Current.Resolve<IUIThreadDispatcher> ().RunLater (delegate {
if (modalStack.Count == 0)
root.DismissModalViewControllerAnimated (animated);
else
modalStack.Peek ().DismissModalViewControllerAnimated (animated);
});
}
public void PushView (UIViewController viewController, bool animated = true)
{
Util.RunOnUIThread (delegate {
UINavigationController navController;
if (modalStack.Count == 0)
navController = root.SelectedViewController as UINavigationController;
else
navController = modalStack.Peek () as UINavigationController;
if (navController == null)
throw new InvalidOperationException ("Can only push on a navigation-controller");
navController.PushViewController (viewController, animated);
});
}
public void PopView (bool animated = true)
{
Util.RunOnUIThread (delegate {
UINavigationController navController;
if (modalStack.Count == 0)
navController = root.SelectedViewController as UINavigationController;
else
navController = modalStack.Peek () as UINavigationController;
if (navController == null)
throw new InvalidOperationException ("Can only pop on a navigation-controller");
navController.PopViewControllerAnimated (animated);
});
}
public void PopToView (UIViewController viewController, bool animated = true)
{
Util.RunOnUIThread (delegate {
UINavigationController navController;
if (modalStack.Count == 0)
navController = root.SelectedViewController as UINavigationController;
else
navController = modalStack.Peek () as UINavigationController;
if (navController == null)
throw new InvalidOperationException ("Can only pop on a navigation-controller");
navController.PopToViewController (viewController, animated);
});
}
public void PopToRoot (bool animated = true)
{
Util.RunOnUIThread (delegate {
UINavigationController navController;
if (modalStack.Count == 0)
navController = root.SelectedViewController as UINavigationController;
else
navController = modalStack.Peek () as UINavigationController;
if (navController == null)
throw new InvalidOperationException ("Can only pop on a navigation-controller");
navController.PopToRootViewController (animated);
});
}
public UIViewController TopViewController
{
get
{
UIViewController ret = null;
Util.RunOnUIThread (delegate {
UIViewController vc;
UINavigationController navController;
if (modalStack.Count == 0)
vc = root;
else
vc = modalStack.Peek ();
if (vc is UINavigationController) {
navController = (UINavigationController)vc;
ret = navController.ViewControllers[navController.ViewControllers.Length - 1];
} else {
ret = vc;
}
});
return ret;
}
}
}
}
public static class ExtensionMethods
{
public static UINavigationController Wrap (this DialogViewController dvc)
{
return new UINavigationController (dvc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment