Programmatically Go Back to Previous Viewcontroller in Swift

Programmatically go back to previous ViewController in Swift

Swift 3:

If you want to go back to the previous view controller

_ = navigationController?.popViewController(animated: true)

If you want to go back to the root view controller

_ = navigationController?.popToRootViewController(animated: true)

If you are not using a navigation controller then pls use the below code.

self.dismiss(animated: true, completion: nil)

animation value you can set according to your requirement.

iOS how to simple return back to previous presented/pushed view controller programmatically?

You can easily extend functionality of any inbuilt classes or any other classes through extensions. This is the perfect use cases of extensions in swift.

You can make extension of UIViewController like this and use the performSegueToReturnBack function in any UIViewController

Swift 2.0

extension UIViewController {
func performSegueToReturnBack() {
if let nav = self.navigationController {
nav.popViewControllerAnimated(true)
} else {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}

Swift 3.0

extension UIViewController {
func performSegueToReturnBack() {
if let nav = self.navigationController {
nav.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: nil)
}
}
}

Note:

Someone suggested that we should assign _ = nav.popViewControllerAnimated(true) to an unnamed variable as compiler complains if we use it without assigning to anything. But I didn't find it so.

Programmatically go back to consecutive 3rd previous ViewController in Swift

Plug this into a playground and mess around with it:

import UIKit

protocol DismissDelegate: AnyObject {
func dismissVC(_ presenting: Int)
}

class VC1: UIViewController, DismissDelegate {
func dismissVC(_ presenting: Int) {
guard presenting == 1 else {
return
}
print("dismiss 2, 3, 4, 5")
}
}
class VC2: UIViewController, DismissDelegate {
weak var vc2Delegate: DismissDelegate?
func dismissVC(_ presenting: Int) {
guard presenting == 2 else {
return vc2Delegate!.dismissVC(presenting)
}
print("dismiss 3, 4, 5")
}
}
class VC3: UIViewController, DismissDelegate {
weak var vc3Delegate: DismissDelegate?
func dismissVC(_ presenting: Int) {
guard presenting == 3 else {
return vc3Delegate!.dismissVC(presenting)
}
print("dismiss 4, 5")
}
}
class VC4: UIViewController, DismissDelegate {
weak var vc4Delegate: DismissDelegate?
func dismissVC(_ presenting: Int) {
guard presenting == 4 else {
return vc4Delegate!.dismissVC(presenting)
}
print("dismiss 5")
}
}
class VC5: UIViewController {
weak var vc5Delegate: DismissDelegate?
func dismissStack(at presenting: Int) {
vc5Delegate?.dismissVC(presenting)
}
}

let vc1 = VC1()

let vc2 = VC2()
vc2.vc2Delegate = vc1

let vc3 = VC3()
vc3.vc3Delegate = vc2

let vc4 = VC4()
vc4.vc4Delegate = vc3

let vc5 = VC5()
vc5.vc5Delegate = vc4

vc5.dismissStack(at: 1) // prints: dismiss 2, 3, 4, 5
vc5.dimissStack(at: 2) // prints: dismiss 3, 4, 5

The setup is very basic, each view controller is chained through a delegate. Because there is more than one view controller, we created a protocol. That protocol is just a method that takes an integer argument which is the view controller that should perform the dismiss (the presenting view controller). Then when you want to perform a dismiss, call the delegate (doesn't have to be from the 5th view controller), specify the presenting view controller, and the delegate will go down the chain and check if it's the presenting view controller (if it is, it will dismiss everything above it) or not (if it isn't, it will step down one view controller and repeat the process).

VC5 has a method called dismissStack(at:) which lets you put in an integer of the view controller you want to dismiss at. Therefore, if you dismiss at 2, then view controllers 3, 4, 5 will be dismissed. You can, obviously, place this method into the other view controllers as well.

Navigate Back to previous view controller

You need to use:

[self.navigationController popToRootViewControllerAnimated:YES];

This will bring you back to the root view controller.

If you want to navigate back to previous view controller, you should implement:

[self.navigationController popViewControllerAnimated:YES];

Navigation Back to another View Controller instead of from which it appeared

You should remove B from your navigation controller's viewControllers array in C's viewDidAppear method.

var didRemoveB = false

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !didRemoveB {
navigationController?.viewControllers.remove(atIndex: 1) //assuming B's index is 1
didRemoveB = true
 }
}

Popping out storyboard and moving back to previous viewcontroller (nib)

Use dismiss(animated:) method.

@IBAction func goBack(sender: UIButton) {
dismiss(animated: true)
}


Related Topics



Leave a reply



Submit