Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i handle navigation Back button view hierarchy?

Tags:

xcode

ios

swift

Here is the problem

1) Rootview controller - MYAssetVC-> Embedded with NavigationController here pushing for button to another Addfilevc.

2) Addfilevc Has dropdownTextfield It will push to another vc have tableview selected row will display in the textfield.

3)if i select another value from the dropdown textfield it will push to the vc again there i will select.

Navigation bar back button will navigate to all my view hierarchy i want to handle this one. if i go to same view it should navigate back only once that to the recent visit how to do this.

As i am new to iOS. give any suggestion.

Navigation from 1->2->3

navigation backbtn 3->2->1

if i navigate like this 1->2->3-> backbutton 3->2 again 2->3 backbutton 3->2 again 2->3

IF i navigate now using back it is displaying all my route path it should navigate like 1->2->3> and 3->2->1 if any number of times i perform actions in 2 & 3.

1,2,3 are view controllers.

like image 588
Naveen Avatar asked Nov 17 '25 18:11

Naveen


1 Answers

Create an IBAction for the back button and use popViewController.

[self.navigationController popViewControllerAnimated:YES];

This will help you to go back one page. You have to write this in all the pages where there is a back button and you want to go back one page.

If you want to go back directly to rootViewController, try this:

[self.navigationController popToRootViewControllerAnimated:YES];

And if you want to pop to any specific viewController in the stack, you run a for loop to find the viewController you want to navigate to and then simply popToViewController, like this:

for (UIViewController *viewController in self.navigationController.viewControllers) {
    if ([viewController isKindOfClass:[Addfilevc class]]) {
        [self.navigationController popToViewController:viewController animated:YES];
    }
}

Hope this helps to clear your concept.

EDIT

In swift:

  • The [self.navigationController popViewControllerAnimated:YES]; will become self.navigationController?.popViewControllerAnimated(true)

  • The [self.navigationController popToRootViewControllerAnimated:YES]; will become navigationController?.popToRootViewControllerAnimated(true)

  • And the for loop will be as below:

You can use this if you are using storyboard

let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as ComposeViewController

self.navigationController?.pushViewController(switchViewController, animated: true)

And the for-in loop

if let viewControllers = self.navigationController?.viewControllers {
    for viewController in viewControllers {
        self.navigationController!.popToViewController(viewController, animated: true);
    } 
}

Thanks.

like image 116
try catch finally Avatar answered Nov 19 '25 08:11

try catch finally



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!