How to pop VC to jump previous two View Controllers using Swift Language?
You are popping (going back to previous controller) instead of pushing (adding new) view controller. What you should do is:
@IBAction func actSingUp(_ sender: Any) {
let storyboard = UIStoryboard(name: "LoginSB", bundle: nil)
let signupvc = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LoginVC
// this step is optional, it will remove SignUp controller from navigation stack
navigationController?.popViewController(animated: false)
// present login controller
navigationController?.pushViewController(signupvc, animated: true)
}
Swift & Navigation : How do I pop until a certain ViewController?
The navigation controller holds a group of view controller and you can pop to any sub-controller you want. For example your flow seem like:
HomeViewController
->ContactViewController
->ContactDetailsViewController
->ChatViewController
Then from ChatViewController
you want to push back to ContactViewController
:
class ChatViewController: UIViewController {
....
func popToContact() {
if let contactVC = self.navigationController?.viewControllers.filter { $0 is ContactViewController }.first {
self.navigationController?.popToViewController(contactVC, animated: true)
}
}
}
How to pop previous view controller from current view controller?
Implement the following in your login view controller:
override func viewWillDisappear(_ animated: Bool) {
var navigationArray = navigationController?.viewControllers
let count = navigationArray?.count
navigationArray?.remove(at: count! - 2)
navigationController?.viewControllers = navigationArray!
}
This removes the current view controller(the login VC in your case) from the navigation stack just when it is about to disappear. So when you tap the back button from the next VC it will always take you to the VC preceding the login.
How to pop the previous view controller from a navigation stack?
It's really much simpler:
self.navigationController?.popViewController(animated: true)
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.
How to pop view controller to one of the previous view controllers in swift?
Instead of doing a generic popViewControllerAnimated:
call, use popToViewController:animated:
. You could detect if the user has deleted all of the rows in which case, do something like this (otherwise just pop one view controller):
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 2], animated: true);
How to access previous view controller from a dismiss command
First of all, It's not necessary to access the previous ViewController to reload tableview(or any other func)
I recommend you to use Delegate to achieve the same feature.
Holding a reference to the previous viewController in the way you mentioned will make your app very hard to maintain when your app gets more complicated.
Double return to previous view - Swift
If A
is always the first view controller, you can just do :
viewcontrollerC.navigationController?.popToRootViewController(animated: true)
This methods pop the stack to the first view controller, without displaying intermediates ones
If A
is not the first viewController, you can do :
viewcontrollerC.navigationController?. popToViewController(viewControllerA, animated: true)
If you don't have a reference to viewControllerA
, search it in the stack :
let viewControllerA: UIViewController?
for (let vc in (self.navigationController?.viewControllers ?? [])) {
//adust the test to find the appropriate controller
if vc.isKindOf(ViewControllerAClass.self) {
viewControllerA = vc
break
}
}
if let viewControllerA = viewControllerA {
self.navigationController?.popToViewController(viewControllerA, animated: true)
}
source : https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller
Related Topics
Xcode 8 with Mixed Swift and Objective-C Project Generated "Modulename-Swift.H" Header Not Found
Has Anyone Found a Good Way of Using the New iOS5 Keyboard Events
Is It Safe to Delete SQLite's Wal File
How to List (Almost) All Emojis in Swift for iOS 8 Without Using Any Form of Lookup Tables
iPad - Parsing an Extremely Huge JSON - File (Between 50 and 100 Mb)
Perform UI Changes on Main Thread Using Dispatch_Async or Performselectoronmainthread
iOS 10 Rich Media Push Notification (Media Attachment) in Objective-C
Restkit and Saving to Coredata as Nsmanagedobject
Drawing Gradient Over Image in iOS
Getting a Thumbnail of a *.Mov Video iOS
iPhone - Didselectrowatindexpath: Only Being Called After Long Press on Custom Cell
I am Getting Unsupported Parameter Combination Cgbitmap Error with Swift
How to Disable Arc for a Single File in Xcode 5
Could Not Attach to Pid:"####" Unable to Attach
Xcode 7 UI Testing: Dismiss Push and Location Alerts
How to Make Your Push Notification Open a Certain View Controller