Reputation: 9479
If you have four view controllers
A B C D
And the root navigation controller points at A. On view controllers A, B and C, there is a button that navigates you to the right. Such that the flow of the app is
root -> A -> B -> C -> D
Is there a way such that the back button on view controller D
can pop 2 view controllers and take me back to view controller B
?
Upvotes: 1
Views: 1308
Reputation: 3436
Yes, You can do this.
Find out view controller from Stack to which you want to pop. then use PopToViewController to pop back to That viewcontroller.
write following code on backbutton handler in D view controller:
this.NavigationItem.SetLeftBarButtonItem (new UIBarButtonItem(
UIImage.FromFile("back.png"), UIBarButtonItemStyle.Plain, (sender, args) => {
var vwControllers = this.NavigationController.ViewControllers;
foreach(UIViewController vc in vwControllers) {
if (vc.GetType () == typeof (B)) {
this.NavigationController.PopToViewController (vc);
}
}), true);
Upvotes: 2
Reputation: 9479
While this might not be the ideal ui flow (you see view controller 3 when unwinding to view controller B), I got it to work. For this to work, you must name your segues in your storyboard file (you must set each's Identifier).
You are able to pop back more than 2 view controllers if you create this chain further. The only caveat of using this method is the ui transition.. hope this helps others!
View controller B
public partial BViewController : UIViewController
{
public BViewController(IntPtr handle) : base(handle)
{ }
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue(segue, sender);
// Add to view controller stack;
// segue Identifier (in storyboard) must match string comparison below
if (segue.Identifier == "ToViewControllerCSegue")
{
var cViewController = segue.DestinationViewController as CViewController;
if (bViewController != null)
{
cViewController.ViewControllerStack = new List<UIViewController>();
cViewController.ViewControllerStack.Add(this);
}
}
}
// Other methods you want to have
}
View controller C
public partial CViewController : UIViewController
{
public CViewController(IntPtr handle) : base(handle)
{ }
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue(segue, sender);
// Add to view controller stack;
// segue Identifier (in storyboard) must match string comparison below
if (segue.Identifier == "ToViewControllerDSegue")
{
var dViewController = segue.DestinationViewController as DViewController;
if (dViewController != null)
{
ViewControllerStack.Add(this);
dViewController.ViewControllerStack = ViewControllerStack;
}
}
}
public List<UIViewController> ViewControllerStack { get; set; }
// Other methods you want to have
}
View controller D
public partial DViewController : UIViewController
{
public DViewController(IntPtr handle) : base(handle)
{ }
public override void DidMoveToParentViewController(UIViewController parent)
{
base.DidMoveToParentViewController(parent);
// parent will be null when clicking the back button
if (parent == null)
{
// Pops back to view controller b;
// you could pop back further if you had more
// viewcontrollers in the List<>
ViewControllerStack[ViewControllerStack.Count - 1].NavigationController.PopToViewController(ViewControllerStack[0], true);
// Refreshes view
ReloadInputViews();
}
}
public List<UIViewController> ViewControllerStack { get; set; }
// Other methods you want to have
}
Upvotes: 0
Reputation: 131481
I have no idea about the Xamarine part of it, but for native iOS apps, there is the UINavigationController
method popToViewController(_:animated:)
that will pop an arbitrary number of view controllers off the stack to expose the specified view controller. (That's the Swift signature. The Objective-C method signature is popToViewController:animated:
.)
I assume there is a way to get to that method from Xamarin?
Upvotes: 0