Reputation: 4734
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. I'm trying to develop an app with Xcode 6.3 and 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. When I click on a button inside D I want to go to B. This is fine. But I would like also to display the top navigation bar with a back button pointing to 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.
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 ?
If you know the code to achieve this in objective-c it's fine for me as well.
Upvotes: 0
Views: 3094
Reputation: 4734
Here is the solution from nburk converted in swift with the use of 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.
Upvotes: 1
Reputation: 22731
When I click on a button inside D I want to go to B. This is fine. But I would like also to display the top navigation bar with a back button pointing to A.
You can only go back to A, if it is actually on the navigation stack. 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.
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. So, logically, what you have to do is make sure that A is on the navigation stack just before B. I don't think you can do this simply with segues from IB, but rather use code and instantiate the UINavigationController
yourself:
- (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.
Upvotes: 2