xcode 在导航控制器中的视图之间跳转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15005736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jumping between views in Navigation controller
提问by Dim
I need to jump between view controllers. For example:
我需要在视图控制器之间跳转。例如:
View1: First screen (Just logo)
View2: Download Screen
View3: First app screen (Some Buttons)
View4-View(N): some app screens
When user enters app the app goes to View1->View2 (downloads stuff)->View 3->View4->View5 Then user wish to go to First app screen (View3) he does:
当用户进入应用程序时,应用程序转到 View1->View2(下载内容)->View 3->View4->View5 然后用户希望转到第一个应用程序屏幕(View3),他这样做:
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];
The first time user enters the app it goes: View1->View3 (The download screen no longer needed), and I have different push segue to go to View3 so lets assume the user goes to: View1->View 3->View4->View5, now he wishes to go back to View3, so the function:
用户第一次进入应用程序时,它会转到:View1->View3(不再需要下载屏幕),并且我有不同的 push segue 可以转到 View3,所以假设用户转到:View1->View 3->View4- >View5,现在他想回到View3,所以函数:
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];
Will return him to View4, and this is WRONG. How can I solve it?
会让他回到 View4,这是错误的。我该如何解决?
采纳答案by u.gen
Well if you are using storyboards , what you can do is set your uiviewcontrollers' storyboard id and use it for popping and pushing your views.
好吧,如果您使用的是 storyboards ,您可以做的是设置您的 uiviewcontrollers 的故事板 ID 并使用它来弹出和推送您的视图。
Lets say your Storyboards name is MainStoryboard.storyboard
假设您的故事板名称是 MainStoryboard.storyboard
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
SettingsListViewController *settingsVC = [sb instantiateViewControllerWithIdentifier:@"SettingsListViewController"]; // @"SettingsListViewController" is the string you have set in above picture
[self.navigationController popToViewController:settingsVC animated:YES];
Above solution should work for you , but If I were you I would change the structure of my app , you say :
上面的解决方案应该对你有用,但如果我是你,我会改变我的应用程序的结构,你说:
View1: First screen (Just logo)
View2: Download Screen
Since View1 is just logo and View 2 is also a view that you skip, you can remove them from navigation controller and make View3 your navigation controller's root view controller
and when you need view1 and view2 present them as Modal View Controllers
,
由于 View1 只是徽标,而 View 2 也是您跳过的视图,您可以将它们从导航控制器中删除,并使 View3 成为您的导航控制器root view controller
,当您需要 view1 和 view2 时present them as Modal View Controllers
,
when you are done with them lets say; user successfully loaded the app dismiss logo screen and present Download Screen
if download successful then dismiss it.
当你完成它们时,让我们说;用户成功加载应用程序关闭徽标屏幕,present Download Screen
如果下载成功则关闭它。
So your View3 Will be there as root view controller, lets say your at View(n) you want to go to home screen which is View3 all you need to do is call
所以你的 View3 将作为根视图控制器在那里,假设你在 View(n) 你想要去主屏幕 View3 你需要做的就是调用
[self.navigationController popToRootViewControllerAnimated:NO];
When you are on view(n)
and want to pop to view(n-1)
just use
当您打开view(n)
并想要弹出以view(n-1)
使用时
[self.navigationController popViewControllerAnimated:YES];
good luck,
祝你好运,
回答by Aaron
It sounds like View 2 is not being added to your view controllers array at run time, possibly because of the segue you've created.
听起来 View 2 没有在运行时添加到您的视图控制器数组中,可能是因为您创建了 segue。
Try removing the segue that transitions from View 1 > View 3 and pushing the user past View 2 without animating, as your application's logic requires it:
尝试删除从 View 1 > View 3 过渡的 segue 并将用户推送到 View 2 而不进行动画,因为您的应用程序的逻辑需要它:
// If the user needs to skip ahead to view 3, conditionally push view 2 and view 3 without animating
[self.navigationController pushViewController:viewController2 animated:NO];
[self.navigationController pushViewController:viewController3 animated:NO];
Alternatively if you left the segue in place could you not look at the size of the UINavigationController
viewControllers
property and "guess" based on the size whether or not you skipped View 2? If you did then you can adjust the popToViewController
method to pop to the correct index. This is, admittedly less elegant and brittle if you need to skip other views as well.
或者,如果您将 segue 留在原地,您是否可以不查看UINavigationController
viewControllers
属性的大小并根据大小“猜测”是否跳过了视图 2?如果你这样做了,那么你可以调整popToViewController
方法以弹出到正确的索引。如果您还需要跳过其他视图,这无疑不那么优雅和脆弱。
// Check length of viewController array with 'N' views (pseudo code)
if (self.navigationController.viewControllers.length == N-1)
// View 2 was ignored: pop to objectAtIndex:1
else
// View 2 was included: pop to objectAtIndex:2
回答by Timur Bernikovich
If I understood you correctly, your view3 has special view controller, so you can use such code:
如果我理解正确的话,你的 view3 有特殊的视图控制器,所以你可以使用这样的代码:
NSArray *VCs = [self.navigationController viewControllers];
for (UIViewController *VC in VCs)
{
if ([VC isKindOfClass:[**YOUR-VIEW-CONTROLLER** class]]) {
[self.navigationController popToViewController:VC animated:NO];
}
}
It's simple and it works!
这很简单,而且有效!
回答by Ravindra Bagale
according to your sitation use directly name of viewController
根据您的情况,直接使用 viewController 的名称
create instance of your viewController,like this i supposed here that your viewController name is-view3Controller
创建你的viewController的实例,像这样我在这里假设你的viewController名称是-view3Controller
View3Controller view3Controller=[[View3Controller alloc]init];
[self.navigationController popToViewController:@"view3Controller" animated:NO]
or if you are using storyboard then
或者如果您正在使用故事板,那么
View3Controller view3Controller=[self.storyboard instantiateViewControllerWithIdentifier:@"view3Controller"];
[self.navigationController popToViewController:@"view3Controller" animated:NO]
回答by Rakesh
In the second sequence your navigation stack changes and view3 would be at index 1. so
在第二个序列中,您的导航堆栈发生变化,view3 将位于索引 1。所以
[[self.navigationController popToViewController:[array objectAtIndex:1] animated:NO];
would be the right way of doing it.
将是正确的做法。
回答by SanjiRambo007
I always use this, and it will work fine in your case. In fact the following line of code is copied to my "Notes" for quick copy/paste.
我总是使用这个,它会在你的情况下正常工作。事实上,以下代码行已复制到我的“注释”中,以便快速复制/粘贴。
Be sure to import your ViewController.h file, if not.
如果没有,请确保导入您的 ViewController.h 文件。
for (UIViewController *viewController in self.navigationController.viewControllers) {
if([viewController isKindOfClass:["your ViewController" class]]) {
[self.navigationController popToViewController:viewController animated:NO];
}
}
回答by Ashwini Chougale
for (UIViewController* controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[<Your View Controller Name> class]]) {
[self.navigationController popToViewController:controller animated:YES];
return;
}
}
回答by shyju
Go like this to a particular view controller
像这样转到特定的视图控制器
[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:1] animated:YES];