Navigation Controller Loop

前端 未结 2 1542
花落未央
花落未央 2020-12-04 01:06

I\'m making an app which has a shopping cart feature. The shopping cart VC is accessible from several VCs via a button on the navigation bar. So far I only have one

相关标签:
2条回答
  • 2020-12-04 01:24

    Awesome.

    In these scenarios you should use setViewControllers([UIViewController], animated: Bool) to get the desired viewcontrollers in the stack when you get many controllers stacked up in cycles.

    Other way is you write your own class derived from UINavigationController having methods, pushToCheckout(animated:Bool), popToEditCart(animated:Bool) , removeIntermediateControllers()

    0 讨论(0)
  • 2020-12-04 01:26

    It's possible to achieve this kinda transition.

    Here, I describe how to implement this with your chart. As your chart shows, assume you have VC1, VC2, VC3 (top to bottom) and VCX (w/ blue box). You need to define transitions and associated animation directions (push: right-to-left or pop:left-to-right). As your chart, if you define the transitions and animations as:

    1. VC1 to : VC2(push), VCX(push)
    2. VC2 to : VC3(push), VCX(push)
    3. VC3 to : VCX(push)
    4. VCX to : VC3(pop)

    and assume we have all of the view controllers instantiated already, then,

    • VC1 to VC2 transition

    at VC1:

    navigationController!.pushViewController(VC2, animated: true)
    

    at VC2:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // remove VC1 from navigation stack (I assume VC1 is not a root VC)
        let allControllers = NSMutableArray(array: navigationController!.viewControllers)
        allControllers.removeObject(at: allControllers.count - 2)
        navigationController!.setViewControllers(allControllers as [AnyObject] as! [UIViewController], animated: false)
     }
    
    • VC1 to VCX transition

    at VC1:

    navigationController!.pushViewController(VCX, animated: true)
    

    at VCX: (A)

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let allControllers = NSMutableArray(array: navigationController!.viewControllers)
        if (navigationController!.viewControllers[allControllers.count-2] != VC3) {
            // if not from VC3, remove VC from stack and put VC3
            allControllers.removeObject(at: allControllers.count - 2)
            allControllers.insert(VC3, at: allControllers.count - 1)
            navigationController!.setViewControllers(allControllers as [AnyObject] as! [UIViewController], animated: false)
        }
    
    • VC2 to VC3 transition

    at VC2:

    navigationController!.pushViewController(VC3, animated: true)
    

    at VC3: (B)

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let allControllers = NSMutableArray(array: navigationController!.viewControllers)
        if (navigationController!.viewControllers[allControllers.count-2] == VC2) {
            // if from VC2, remove it
            allControllers.removeObject(at: allControllers.count - 2)
            navigationController!.setViewControllers(allControllers as [AnyObject] as! [UIViewController], animated: false)
        }
    }
    
    • VC2 to VCX transition

    at VC2:

    navigationController!.pushViewController(VCX, animated: true)
    

    at VCX: same as (A)

    • VCX to VC3 transition

    at VCX:

    navigationController!.popViewController(animated: true)
    

    at VC3: same as (B)

    Note viewDidAppear is called when users swipe (left-to-right) to go back and cancel it on the way (== swipe back to left). So, you need some more small code at viewDidAppear against that situation.

    If you want the different animation direction, by manipulating stack and using push/pop, you can easily achieve it. This explains how.

    0 讨论(0)
提交回复
热议问题