[英]Push view to navigation from modal of another tab
I'm quite new to iOS programming and I'm trying to do something that I think is simple but I can't find clear explanation. 我是iOS编程的新手,我正在尝试做一些我认为很简单的事情,但是找不到明确的解释。 I'm trying to develop an app with Xcode 6.3 and swift 1.2
我正在尝试使用Xcode 6.3和Swift 1.2开发应用程序
I have this : 我有这个 :
So I'm in a tab based application. 因此,我处于基于选项卡的应用程序中。 Each tab view are inside navigation controller.
每个选项卡视图都在导航控制器内部。 D is a modal launch from C .
D是C的模式发射。 When I click on a button inside D I want to go to B .
当我单击D内的按钮时,我想转到B。 This is fine.
这可以。 But I would like also to display the top navigation bar with a back button pointing to A .
但我也想显示顶部导航栏,并带有指向A的后退按钮。 For now when I tried to use the push segue from D to B , B is shown as modal but not part of the navigation controller.
现在,当我尝试使用从D到B的推送功能时, B显示为模态,但不是导航控制器的一部分。
How I have to achieve this easily ? 我要如何轻松实现这一目标? Do I have to recreate all the stack by instantiating each view (A then B) and push everything onto the navigation stack ?
我是否必须通过实例化每个视图(从A到B)来重新创建所有堆栈并将所有内容推送到导航堆栈上?
If you know the code to achieve this in objective-c it's fine for me as well. 如果您知道在Objective-C中实现此目标的代码,对我也很好。
When I click on a button inside D I want to go to B . 当我单击D内的按钮时,我想转到B。 This is fine.
这可以。 But I would like also to display the top navigation bar with a back button pointing to A .
但我也想显示顶部导航栏,并带有指向A的后退按钮。
You can only go back to A , if it is actually on the navigation stack . 如果它实际上在导航堆栈中 ,则只能返回 A。 So, you will have to somehow push it onto the stack before you can achieve your goal.
因此,您必须以某种方式将其推入堆栈,然后才能实现目标。
As a general hint, if you want a view controller to show the navigation bar, you only have to make sure it's embedded into a navigation controller. 作为一般提示,如果希望视图控制器显示导航栏,则只需确保将其嵌入到导航控制器中即可。 When you present a view modally, it will not by default be embedded into a navigation controller.
模态显示视图时,默认情况下不会将其嵌入到导航控制器中。 So, as you said D is shown modally from C , just embed D into another navigation controller and instead of making D the destination for the modal segue, make the navigation controller the destination.
因此,正如您所说的, D是从C模态显示的,只是将D嵌入到另一个导航控制器中,而不是将D用作模态搜索的目的地,而是将导航控制器作为目的地。
Update 更新资料
If you want to show a back button in B that points to A , even though B was shown modally from D , you will have to hack your way around because A is not on the navigation stack. 如果要在B中显示一个指向A的后退按钮, 即使B是从D模态显示的 ,则由于A不在导航堆栈中,您将不得不四处寻找。 So, logically, what you have to do is make sure that A is on the navigation stack just before B .
因此,从逻辑上讲,您要做的是确保A在导航堆栈上紧挨B。 I don't think you can do this simply with segues from IB, but rather use code and instantiate the
UINavigationController
yourself: 我不认为您可以仅使用IB的segue来做到这一点,而是使用代码并自己实例化
UINavigationController
:
- (void)showBModallyWithBackButtonToA
{
A *a = [[A alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:a];
B *b = [[B alloc] init];
[nav pushViewController:b animated:NO];
[self presentViewController:nav animated:YES completion:nil];
}
This code has to be executed from D , if I understand your setup correctly. 如果我正确理解了您的设置,则必须从D执行此代码。
Here is the solution from nburk converted in swift with the use of storyboard : 这是nburk通过使用Storyboard快速转换的解决方案:
let a = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("aIdentifier") as! ATableViewController
let nav = UINavigationController(rootViewController: a)
let b = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("bIdentifier") as! BViewController
// b parameters here
// b.parameter1 = variable
nav.pushViewController(b, animated:false)
self.presentViewController(nav, animated:true, completion:nil)
Where aIdentifier and bIdentifier are the identifier you set inside storyboard editor for each view and ATableViewController and BViewController their respective ViewController classes. 其中aIdentifier和bIdentifier是您在故事板编辑器中为每个视图设置的标识符,而ATableViewController和BViewController分别是其各自的ViewController类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.