I have hierarchy of ViewControllers in my storyboard structure.
It is A-B-C-D. A is embed with NavigationController and the flow goes on till D viewController. All fours view attached through segues. Now I am on D viewController, I defined some action to the button of D that It should take me directly to A viewController that is rootViewController or B viewController. Then how can I achieve this. I tried everything but didn't succeed.
I want something like it should not disturb A-B-C-D flow and it should take me to A viewController from D.
Right click on your D viewcontroller and drag i to your A viewcontroller.
Then click on the object which appears on the line you just created.
Write something like DtoA in the storyboard segue identifier in the attributes inspector.
Now in D view controller, just do:
[self performSegueWithIdentifier:#"DtoA" sender:self];
And if you instead wish to pop to a previous viewcontroller the old fashioned way, like from D to B:
UINavigationController* navController = self.navigationController;
UIViewController* Bviewcontroller = [navController.viewControllers objectAtIndex:1];
[navController popToViewController:controller animated:YES];
I hope this helps!
Related
Suppose I have 3 controllers namely A, B and C. A to B I have a push segue. And I have a push segue from B to C. Now if I have to make a segue directly from A to C then without going to the B controller how can I achieve this?
Thanks
you can manually push that view controller in navigationController like this
UINavigationController *navigation = [self navigationController];
after this just insert your b View in this navigationController like this
[navigation pushViewController:yourController animatte:yes];
In your case you can setup the view controllers as follows :
Navigation_Controller -> VC_A -> VC_B -> VC_C
You can create the view controllers using storyboards, and then give each of your view controllers as storyboard ID.
Now you can navigate to your desired VC, as follows :
- (IBAction)goToB:(UIButton *)sender {
VCB *theVCB = [self.storyboard instantiateViewControllerWithIdentifier:#"B"];
[self.navigationController pushViewController:theVCB animated:YES];
}
- (IBAction)goToC:(UIButton *)sender {
VCC *theVCC = [self.storyboard instantiateViewControllerWithIdentifier:#"C"];
[self.navigationController pushViewController:theVCC animated:YES];
}
Note : You could achieve the same result by connecting VCB and VCC using segues but that is not advisable.
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.
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.
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.
My storyboard structure is like:
A is a navigation controller
B is a tab bar controller
C,D,E are view controllers
F,G,H,I,J are a view controllers
if now i am on I ,and there's a button the i pressed then I go back to C.How to do that?
I tried make segue between I and C, but C has a back button, you pressed it,you back to I.
I don't want that.when i came from I to C, i want C is as I first come to C from B.
if i want to go to H from I,I want H have a back button that you pressed and you back to F not to I.
From I to C: do popViewController twice, or loop through the navigationcontroller's viewcontrollers and find C, pop to C.
From H to I: Programmatically push to I. In storyboard set an storyboard ID for I, and you can create a I instance through [storyboard instantiateViewControllerWithIdentifier:ID];
I would set the viewControllers property of UINavigationController.
Create an array of View Controllers like you would desire the stack to be. Example: #{C, F, I} or in the case of your question you would use #{ C } then update the navigationController to contain these views.
[self.navigationController setViewControllers:(NSArray *)];
If you want to do something other than what your Segues are set up for, be sure to give your VC a stoyboard ID
Then using that storyboard ID, you can call this code to create/display;
-(IBAction)myButtonAction:(id)sender{
RDLaunchOptionsTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"RDLaunchOptionsTableViewController"];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
You don't have to use a nav controller. Just use [self presentViewController....];
i transferred from View Controller A to View Controller B with this code
UIViewController *HomePageView = [self.storyboard instantiateViewControllerWithIdentifier:#"HomePageView"];
[self.navigationController setViewControllers:#[HomePageView] animated:YES];
// The above code transfers the user from the A to B after verification.
Now i am trying to connect a view controller B to view controller C with a push segue. The button that does the action is a navigation bar button,but anytime i try to click on the button it doesn't respond. I also think I'm have this problem because view controller B is not my initial scene. Any ideas on what i can do?
Please view picture link to have an idea. http://i.stack.imgur.com/ijWjl.png
Try using:
[self presentViewController:HomePageView animated:YES completion:nil];
in place of:
[self.navigationController setViewControllers:#[HomePageView] animated:YES];
(if you don't want to go back from B to A).
In my program have 6 view controllers.(in storyboard)
Lets define 1,2,3,4,5,6.
1 is my main view.
I want to navigate like this(image bellow).Is it possible to do?
give me a idea to do this navigation.
Yes, u can implement this. This is a simple navigation in iOS using NavigationController.
you have six viewControllers 1, 2, 3, 4, 5, 6.
to do this:
First create a NavigationController and initialize it with ViewController 1 (ie a root View Controller).
Now your navigationController behaves like a stack which contains all ur pushed view controller. NavigationController is only push and pop ur view controllers.
So, every time when u want to navigation first check ur viewController is inside navigationController stack or not. If it is already in stack then pop to that controller, if not then push the same view controller.
for this use following:
In case ViewController3
-(void)popToSelectedViewController
{
NSArray *vc=[self.navigationController viewControllers];
ViewController3 *vc3=nil;
for (int i=0; i<[vc count]; i++)
{
UIViewController *tempVC=[vc objectAtIndex:i];
if([tempVC isKindOfClass:[ViewController3 class]])
{
vc=[vc objectAtIndex:i];
break;
}
}
if(vc3)
{
//If exists inside stack the pop
[self.navigationController popToViewController:vc3 animated:YES];
}
else
{
//If not exists inside stack push ViewController3
ViewController3 *vc3New= [[ViewController3 alloc]initWithNibName:#"ViewController3" bundle:nil];
[self.navigationController pushViewController:vc3New animated:YES];
[vc3New release];
}
}
For initializing ur ViewController1 with navigationController:
if using storyboard
Embed ur initialViewController(ie viewController3) with UINavigationController.
for this:
Step1: open storyboard, and select ur initialViewController(ie viewController3).
Step2: Go to Editor in menu -> Choose Embed In -> Select UINavigationController.
this creates a navigationcontroller and initializes with viewController3 as rootViewController.
if not using storyboard
make property of vc3 (ViewController3) and applicationNavigationController (UINavigationController) in .h
and in .m:
got method "application... didFinishedLaunching...." in appDelegate
and write:
self.vc3=[[ViewController3 alloc]initWithNibName:#"ViewController3" bundle:nil];
self.applicationNavigationController=[[UINavigationController alloc] initWithRootViewController:self.vc3];
self.window.rootViewController=self.applicationNavigationController;
[self.window makeKeyAndVisible];
First You create a navigation controller object
UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:rootViewController];
self.window.rootViewController = navCtrl;
If you want to go to 1->2,1->3,1->6,etc, create an object for the next viewcontroller and push it to navigation stack
[self.navigationController pushViewController:second animated:YES];
You dont need to do any additional work to go back to the previous view controller. The default back button lets you go back.
If you need to return to the root view controller, then use this:
[self.navigationController popToRootViewControllerAnimated:YES];
if you want to return to any particular view controller, then use this
[self.navigationController popToViewController:viewController animated:YES];
Use UINavigationController.It is nor circular.It can be assumed as stacked approch.That is what navigation controller do
A must read for you