有人可以解释 UINavigationController setViewController 和 popToViewController

发布于 2024-12-17 04:47:16 字数 629 浏览 0 评论 0原文

我熟悉创建新视图控制器并将其推入堆栈的想法。到目前为止,我刚刚创建了视图控制器的一个实例并推送了它。现在我遇到一个问题,我的 3 个不同的视图控制器是查看数据的相关方式。我的 RootViewController 有 3 个要启动的图标,当您按下一个图标时,您会推送第一个视图控制器。我目前没有在数组中设置视图控制器,因为我不确定这会给我带来什么或者它是如何工作的。

场景1:点击图标1(推送vc1),点击vc1中弹出窗口中的表格,它将推送到vc3。然后你点击vc3中的某个东西,它就会转到vc1。

场景 2:单击图标 3(推送 vc3),单击某个内容并转到 vc1。

所以我遇到的问题是在场景 1 中,弹出回 vc1 似乎是最有意义的,这样他们就没有 vc1/vc3/vc1 堆栈,并且像这样查看数据两次。但在场景 2 中,由于我从 vc3 instd 开始于 vc1,所以我应该推送 vc1。但在这两种情况下,用户都单击相同的内容从 vc3 转到 vc1,那么我如何判断它们来自哪个 viewController 以便推送或弹出到 vc1?

所以我想这就是我认为 setViewControllers 或 popToViewController 可能会派上用场的地方,但我不确定它们是如何工作的,以及是否有人可以提供一些简单的示例片段来帮助我开始(假设这种方法没问题)。谢谢!

I'm familiar with the idea of creating a new viewcontroller and pushing it onto the stack. So far I have just created an instance of my view controller and pushed it. Now I am running into a problem that my 3 different view controllers are related ways of looking at the data. My RootViewController has 3 icons to start, and when you press on an icon, you push the first view controller. I currently do not set the view controllers in an array since I'm not sure what that buys me or how that works.

Scenario 1: Click on icon 1 (push vc1), click on a table in a popover in vc1, it'll push to vc3. Then you click on something in vc3, and it'll go to vc1.

Scenario 2: Click on icon 3 (push vc3), click on something and go to vc1.

So the problem I'm having is in scenario 1, it would seem to make the most sense to pop back to vc1 so they don't have a stack of vc1/vc3/vc1 and are looking at the data twice like that. But in scenario 2, since I started at vc3 instd of vc1, I should push vc1. But in both scenarios, the user is clicking on the same thing to go to vc1 from vc3, so how do I tell which viewController they came from in order to push or pop to vc1?

So I guess that's where I thought setViewControllers or popToViewController might come in handy, but I'm not sure how those work and if there's some simple example snippet someone can provide to get me started (assuming this approach is ok). Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-12-24 04:47:16

从您的描述来看,我觉得您应该使用可以推入和弹出的导航控制器。顺便说一句,导航栏不一定是可见的。

听起来有时您应该在推送下一个 vc 之前弹出当前 vc。您需要检查 viewControllers 数组的计数 - 如果当前计数大于 1,则在推送之前弹出,否则只需在根视图控制器处推送即可。

或者,您可以在每次推送之前使用 popToRootViewControllerAnimated:NO 。

It sounds to me, from your description, that you should be using a navigation controller that you push and pop onto and off. Btw, the navigation bar doesn't have to be visible.

It also sounds like you should sometimes pop the current vc before pushing the next vc. You will need to check the count of the viewControllers array - if the current count is greater than one then pop before pushing, otherwise just push as you are at the root view controller.

Or, you could use popToRootViewControllerAnimated:NO each time before pushing.

微凉 2024-12-24 04:47:16

UINavigationController 允许您管理视图。
setViewController 允许您设置哪个控制器处理您的视图。
popToViewController 将当前视图控制器从堆栈中取出到指定的视图。

IMO...最后两个促进意大利面条逻辑,应该谨慎使用。

UINavigationController is what allows you to manage your views.
setViewController allows you to set which controller handles your view.
popToViewController takes the current view controller off the stack to the specified view.

IMO... The last 2 promote spaghetti logic and should be used sparingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文