导航控制器循环 [英] Navigation Controller Loop
问题描述
我正在制作一款具有购物车功能的应用。购物车VC可通过导航栏上的按钮从多个VC访问。到目前为止,我只有一个导航控制器,如图所示,每个VC都带我到下一个,没什么特别的。第3个VC(指向蓝色箭头)是一个VC,显示产品/项目的详细信息,并允许我将其添加到购物车。
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 Navigation Controller and as shown in the image, each VC takes me to the next one, nothing fancy. The 3rd VC (blue arrow pointing to) is a VC that shows the product/item's details and enables me to add it to the cart.
购物车附带问题VC。要编辑该项目,我想重新使用我之前使用的相同产品/项目的详细信息(蓝色箭头指向)。
Problem comes with the Shopping Cart VC. To edit the item, I'd like to re-use the same product/item's details (blue arrow pointing to) VC I used earlier.
现在,我不要真的有一个问题,但我注意到,一旦我创建了蓝色segue,第三个VC的导航栏就在Storyboard中消失了,但是当我运行应用程序时我仍然可以看到它。
Right now, I don't really have an issue but I have noticed that once I created the blue segue, the Navigation Bar of the 3rd VC disappeared in the Storyboard however I was still able to see it when I ran the app.
注意:
- 图片中的所有段落都是显示
- 购物车VC没有像其他3那样显示自己的按钮。所以在技术上我阻止了购物车VC的无限/循环 - 产品/项目细节VC互相显示。
我的问题是:
- 用VC转到另一个VC并且其他VC可以回到第一个VC那样设计应用程序是不是错了?我是否会在未来遇到问题,或许是内存泄漏?
- 有没有更好的方法来实现我想要实现的目标?
如果有人需要进一步解释,请告诉我,我会编辑我的问题。
If someone needs further explanation please let me know and I'll edit my question.
编辑:为了澄清,蓝色segue基本上是购物车VC中UITableView中的一个按钮。如果您点击该按钮,它应该打开产品/项目详细信息VC并允许您编辑项目的颜色等。在项目详细信息VC中,我不会将项目作为新项目添加到购物车,而是显示编辑按钮,它会将编辑请求发送到API并将VC关闭回购物车,或者我可以使用导航控制器中的后退按钮返回购物车。
To clarify, the blue segue is basically a button in a UITableView in the cart VC. If you tap that button, it should open the product/item details VC and lets you edit the item's color, etc. In the item details VC, instead of adding the item as a new item to the cart, I'd show an Edit button which would send the edit request to the API and dismiss the VC back to the shopping cart, or I could use the back button in the navigation controller to get back to the shopping cart.
EDIT2:@beshio
@beshio
感谢您的回答。然而,VC1实际上是我的根VC,其中所有的应用程序都启动。我没理解为什么从导航控制器的堆栈中删除了VC。我希望Back按钮按预期工作。
Thanks for the answer. However VC1 is actually my root VC where all the app starts. I didn't get why removed VCs from the Navigation Controller's stack. I would like the Back button to work as intended.
到目前为止,我已经实现了我想要的但是我担心让两个VC相互隔离会导致问题。我已经在VC3中禁用了Cart按钮,以防VC3从Cart中出现,因此可以防止循环。我只担心任何内存泄漏。
So far I have achieved what I wanted but I'm afraid that having two VCs segue-ing to each other would cause a problem. I have already disabled the Cart button in VC3 in case VC3 was presented from the Cart so loops would be prevented. I am only worried about any memory leaks down the road.
推荐答案
有可能实现这种转变。
在这里,我将介绍如何使用图表实现此功能。
如图表所示,假设您有VC1,VC2,VC3(从上到下)和VCX(带蓝色框)。您需要定义过渡和关联的动画方向(按下:从右到左或从左到右)。作为您的图表,如果您将过渡和动画定义为:
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:
- VC1到:VC2(推送),VCX(推送)
- VC2到:VC3(推送),VCX(推送)
- VC3到:VCX(推送)
- VCX to:VC3(pop)
- VC1 to : VC2(push), VCX(push)
- VC2 to : VC3(push), VCX(push)
- VC3 to : VCX(push)
- VCX to : VC3(pop)
并假设我们已经实例化了所有视图控制器,然后,
and assume we have all of the view controllers instantiated already, then,
- VC1到VC2转换
在VC1:
navigationController!.pushViewController(VC2, animated: true)
$ b在VC2上
$ b
:
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到VC1转换
在VC1:
navigationController!.pushViewController(VCX, animated: true)
在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到VC3过渡
在VC2:
navigationController!.pushViewController(VC3, animated: true)
在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转换为VCX
在VC2:
navigationController!.pushViewController(VCX, animated: true)
在VCX:与(A)
- VCX到VC3转换
在VCX:
navigationController!.popViewController(animated: true)
$ b VC3上的
$ b
:与(B)相同
at VC3: same as (B)
注意当用户滑动(从左到右)返回时,调用viewDidAppear在途中取消它(==向左滑动)。所以,你需要在viewDidAppear上针对那种情况提供一些更小的代码。
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.
如果你想要不同的动画方向,通过操纵堆栈和使用push / pop,你可以轻松实现它。 这解释了如何。
If you want the different animation direction, by manipulating stack and using push/pop, you can easily achieve it. This explains how.
这篇关于导航控制器循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!