How to test if view controller is dismissed or popped - ios

i want to write an unit test for my function, here is code:
func smartBack(animated: Bool = true) {
if isModal() {
self.dismiss(animated: animated, completion: nil)
} else {
self.navigationController?.popViewController(animated: animated)
}
}
This method automatically chooses dismiss or pop. So, how i can check if viewcontroller popped or dismissed after this function? Thank you for help

You can check the view controller's isBeingDismissed property in either its viewWillAppear or viewDidAppear function.
See https://developer.apple.com/documentation/uikit/uiviewcontroller/2097562-isbeingdismissed.

func smartBack(animated: Bool = true) will be:
func smartBack(animated: Bool = true) {
if self.navigationController?.viewControllers.first == self {
self.dismiss(animated: animated, completion: nil)
} else {
self.navigationController?.popViewController(animated: true)
}
}

You can use property self.isBeingPresented, will return true is view controller presented otherwise false if pushed.

You could check the viewControllers stack and see if your viewController is included or not, using:
self.navigationController.viewControllers
This will return a [UIViewController] contained in the navigationController stack.

Personally I would use Mocks to track when certain methods are called.
You can do this like so:
class MockNavigationController: UINavigationController {
var _popCalled: Bool = false
override func popViewController(animated: Bool) -> UIViewController? {
_popCalled = true
return self.viewControllers.first
}
}
Then anytime your code calls popViewController, the _popCalled value would be updated but it wouldn't actually pop anything. So you can assert the _popCalled value to make sure that the expected call happened.
This makes it easy to test that an expected thing happened and also prevents you running actual code in your tests. This method could easily be a service call, or db update, setting a flag etc, so can be much safer.
They can be difficult to understand at first though. I would suggest reading up on them before heavy use.
A full example in a playground:
import UIKit
import PlaygroundSupport
import MapKit
class ClassUnderTest: UIViewController {
var isModal: Bool = false
func smartBack(animated: Bool = true) {
if isModal {
self.dismiss(animated: animated, completion: nil)
} else {
self.navigationController?.popViewController(animated: animated)
}
}
}
class MockNavigationController: UINavigationController {
var _popCalled: Bool = false
override func popViewController(animated: Bool) -> UIViewController? {
_popCalled = true
return self.viewControllers.first
}
}
class MockClassUnderTest: ClassUnderTest {
var _mockNavigationController = MockNavigationController()
override var navigationController: UINavigationController? {
return _mockNavigationController
}
var _dismissCalled: Bool = false
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
_dismissCalled = true
}
}
var subject = MockClassUnderTest()
subject.isModal = true
subject.smartBack();
var navigation = subject.navigationController as! MockNavigationController
print(subject._dismissCalled)
print(navigation._popCalled)
OUTPUT:
true
false
subject = MockClassUnderTest();
subject.isModal = false
subject.smartBack();
navigation = subject.navigationController as! MockNavigationController
print(subject._dismissCalled)
print(navigation._popCalled)
OUTPUT:
false
true
In this example, you are overriding the dismiss and pop methods which would be called in either case. In your unit test you would just assert the stubbed values (_popCalled) are true or false for your expectations.

I solved in this way. I have needed to test a simple method that contains this: dismiss(animated: true, completion: nil) and I made a temporal mock that simulates a viewController that do a push to my MainController which it is where I apply the dismissView.
func testValidatesTheDismissOfViewController() {
// Given
let mockViewController: UIViewController = UIViewController()
let navigationController = UINavigationController(rootViewController: mockViewController)
// Create instance of my controller that is going to dismiss.
let sut: HomeWireFrame = HomeWireFrame().instanceController()
navigationController.presentFullScreen(sut, animated: true)
// When
sut.loadViewIfNeeded()
sut.closeView()
// Create an expectation...
let expectation = XCTestExpectation(description: "Dismiss modal view: HomeViewController")
// ...then fulfill it asynchronously
DispatchQueue.main.async { expectation.fulfill() }
wait(for: [expectation], timeout: 1)
// Then - if its different of my home controller
XCTAssertTrue(!(navigationController.topViewController is HomeViewController))
}
I hope can help, I´m here to any doubt.

It is worked for me:
func smartBack(animated: Bool = true) {
if self.navigationController == nil {
self.dismiss(animated: animated, completion: nil)
} else {
self.navigationController?.popViewController(animated: true)
}
}

Related

Callback is not working when button tapped

I want to trigger an action on button tap with my callback. Also I have presenter and coordinator. But nothing happenes. My code is not working in this closure:
startViewController.output = { [weak self] action in
switch action {
case .registrationButtonTapped:
self?.showRegistrationViewController()
case .loginButtonTapped:
self?.showLoginViewController()
}
}
In my ViewController I have enum:
enum StartViewControllerButton {
case registrationButtonTapped
case loginButtonTapped
}
callback:
var output: ((StartViewControllerButton) -> Void)?
and selectors:
#objc func registrationButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.registrationButtonTapped)
}
#objc func loginButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.loginButtonTapped)
}
My Presenter
class StartModulPresenter: StartModulPresenterProtocol {
var navigationController: UINavigationController
var coordinator: CoordinatorProtocol?
//Init
init(navigationController: UINavigationController) {
self.navigationController = navigationController
coordinator = AuthorizationCoordinator(navigationController: navigationController)
}
//Functions
func openNextScreen() {
coordinator?.start()
}
}
My Coordinator:
class AuthorizationCoordinator: RegistrationCoordinatorProtocol {
var presenter: PresenterProtocol?
var navigationController: UINavigationController
var childCoordinators: [CoordinatorProtocol] = []
//Init
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func start() {
presenter = StartModulPresenter(navigationController: navigationController)
let startViewController = StartViewController(startModulPresenter: presenter as! StartModulPresenter)
startViewController.output = { [weak self] action in
switch action {
case .registrationButtonTapped:
self?.showRegistrationViewController()
case .loginButtonTapped:
self?.showLoginViewController()
}
}
}
private func showRegistrationViewController() {
let registrationViewController = RegistrationViewController()
registrationViewController.view.backgroundColor = .orange
self.navigationController.pushViewController(registrationViewController, animated: true)
}
private func showLoginViewController() {
let loginViewController = LoginViewController()
loginViewController.view.backgroundColor = .orange
self.navigationController.pushViewController(loginViewController, animated: true)
}
}
Could you check if startViewController is pushed/presented or not?
func start() {
presenter = StartModulPresenter(navigationController: navigationController)
let startViewController = StartViewController(startModulPresenter: presenter as! StartModulPresenter)
startViewController.output = { [weak self] action in
switch action {
case .registrationButtonTapped:
self?.showRegistrationViewController()
case .loginButtonTapped:
self?.showLoginViewController()
}
}
}
And, is self.output is nil or not? If it is nil please check your assignment call, it needed to be called before you use this variable.
#objc func loginButtonPressed() {
startModulPresenter.openNextScreen()
self.output?(.loginButtonTapped)
}
Honestly, I don't recommend you to use this design pattern, just a simple thing but the real result is too complicated.
Just use protocol-based MVC. View communicate with Controller via protocol/closure or Reactive-based with Combine (PassthroughSubject/CurrentValueSubject)

Dismiss View Controller Doesn't Working in Helper Class Swift

So I have a helper class as written below:
class Helper {
static func handleTokenInvalid() {
DispatchQueue.main.async {
UIViewController().dismiss()
}
}
}
extension UIViewController {
func dismiss() {
let root = UIApplication.shared.keyWindow?.rootViewController
root?.dismiss(animated: true, completion: nil) }
}
I want to dismiss all the view controller that open and back to root of the apps. However it doesn't work. If I do the same in ordinary view controller is works. Anyone know the solution? Thank you!
Edit:
I already tried this too, but it said that found nil when wrapping optional value.
func dismiss() {
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
}
All you are doing by this
UIViewController().dismiss
Is creating a new view controller and dismissing it.
You have to call dismiss on the actually presented View controller instance.
After two days finding the right way to dismiss my controller and couldn't find any because I think Xcode find that my current controller is nil. Instead, I use this:
let viewController = UIStoryboard(name: "DashboardPreLogin", bundle: Bundle.main).instantiateViewController(withIdentifier: "TabBarPreLoginViewController")
let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = nil
appDel.window?.rootViewController = viewController
UIView.transition(with: appDel.window!, duration: 0.5, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {() -> Void in appDel.window?.rootViewController = viewController}, completion: nil)
This will dismissing view controller and replace with new controller.
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
You can you use this anywhere on your Helper class
if let topController = UIApplication.topViewController() {
topController.dismiss(animated: true, completion: nil)
}
You can change the rootViewController,
UIApplication.shared.keyWindow?.rootViewController = yourController
I think there is better solution without dismissing root view controller of current application's window.
Add completion handler to your method and when you call it from inside of your controller, in closure declare that after completion will be called, you need to dismiss self (if you're pushing your controller via UINavigationController, just pop it)
static func handleTokenInvalid(completion: #escaping () -> Void) {
DispatchQueue.main.async {
completion()
}
}
Then in controller call your method with completion handler
class ViewController: UIViewController {
func call() {
Helper.handleTokenInvalid { // this is called when you call `completion` from `handleTokenInvalid`
self.dismiss(animated: true)
//self.navigationController?.popViewController(animated: true)
}
}
}

How to present a ViewController modally from the presentingViewController?

I have 3 view controllers.
let's name them as A, B & C.
A presents B and then C should be present from A after dismissing B.
A <=> B
A -> C
How can I achieve this?
If the question is unclear then do let me know, I would be happy to edit it.
Well, I achieved it this way.
Note: I am inside B.
let cViewController = // getting a handle of this view controller from Storyboard
let aViewController = self.navigationController?.presentingViewController
self.dismiss(animated: true) {
aViewController?.present(cViewController, animated: true)
}
You can use custom notification observer like below:
In Controller A:
override func viewDidLoad() {
super.viewDidLoad()
// Register to custom notification
NotificationCenter.default.addObserver(self, selector: #selector(presentC), name: NSNotification.Name(rawValue: "BDismissed"), object: nil)
// Rest of your code
}
func presentC {
// Controller C presentation code goes here
}
In Controller B:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "BDismissed"), object: nil, userInfo: nil)
}
Inside B try this
self.dismiss(animated: true) {
let aVC = UIApplication.shared.keyWindow?.rootViewController as! AVC
let cVC = ///
aVC.present(cVC, animated: true, completion: nil)
}
Write a protocol in B like:
protocol VCBDelegate {
func VCBDismissed()
}
Class VCB: UIViewController {
weak var delegate: VCBDelegate?
....
}
Now where you are dismissing B, call the delegate method in completion.
func dismissB() {
self.dismiss(animated: true) {
self.delegate.VCBDismissed()
}
}
Now conform this protocol in A.
extension VCA: VCBDelegate {
func VCBDismissed() {
//Here you present C
.....
}
}
Don't forget to make the delegate self where you are presenting B.
Hope this helps, for any queries please feel free to leave a comment.
You can use closures, it's better and simple.
Your A will present B and give it a closure to call when it dismiss, this closure will present C.
Here is an example :
class ViewControllerA : UIViewController{
func showViewControllerB(){
let vc = ViewControllerB()
vc.callOnDismiss = { [weak self] in
self?.showViewControllerC()
}
self.present(vc, animated: true, completion: nil);
}
func showViewControllerC(){
let vc = ViewControllerC()
self.present(vc, animated: true, completion: nil);
}
}
class ViewControllerB : UIViewController{
var callOnDismiss : () -> () = {}
func actionOnDismiss(){
self.dismiss(animated: true, completion: nil)
self.callOnDismiss()
}
}
class ViewControllerC : UIViewController{
}

through parameter determine subclass type , but get error " Use of undeclared type ". How can I do?

I am getting below error at this line if vc is targetVC
"Use of undeclared type 'targetVC'"
func popToTargetController(_ targetVC: UIViewController) -> Bool {
guard let currentNv = tabBar?.selectedViewController as? UINavigationController else{
return false
}
for vc in currentNv.viewControllers {
if vc is targetVC {
currentNv.popToViewController(vc, animated: false)
return true
}
}
return false
}
I have declared class like MovieController inherit form UIViewController, and I want pass MovieController as parameter to this method.
I want to use like this:
class MovieController: UIViewController {
....
....
let _ = someModel.popToTargetController(MovieController)
....
}
I think I see what you are trying to do here.
You are trying to find the targetVC in the navigation stack, so that you can pop all the VCs on top of the targetVC.
When you say if vc is targetVC, that makes sense in English. But what you really mean in terms of Swift, is to check that vc and targetVC are the same type of VC.
To fix this, you need to introduce a generic type:
func popToTargetController<T: UIViewController>(_ targetVCType: T.Type) -> Bool {
guard let currentNv = tabBar?.selectedViewController as? UINavigationController else{
return false
}
for vc in currentNv.viewControllers {
if vc is T {
currentNv.popToViewController(vc, animated: false)
return true
}
}
return false
}
And pass your MovieController like this:
popToTargetController(MovieController.self)
Updating answer from comment:
targetVC is not a type. Just your argument name.
func popToTargetController(_ targetVC: MovieController) -> Bool {
guard let currentNv = tabBar?.selectedViewController as?
UINavigationController else {
return false
}
for vc in currentNv.viewControllers {
if vc is MovieController {
currentNv.popToViewController(vc, animated: false)
return true
}
}
return false
}

How can I pop specific View Controller in Swift

I used the Objective-C code below to pop a specific ViewController.
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[AnOldViewController class]]) {
//Do not forget to import AnOldViewController.h
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
How can I do that in Swift?
Try following code:
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
Swift 5
To pop to the latest instance of a specific class, for example SomeViewController:
navigationController?.popToViewController(ofClass: SomeViewController.self)
But you need to add ths UINavigationController extension:
extension UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.last(where: { $0.isKind(of: ofClass) }) {
popToViewController(vc, animated: animated)
}
}
}
For Swift 3+
let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for aViewController in viewControllers {
if aViewController is YourViewController {
self.navigationController!.popToViewController(aViewController, animated: true)
}
}
From Swift 4.0 and Above
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: DashboardVC.self) {
_ = self.navigationController!.popToViewController(controller, animated: true)
break
}
}
This is working Perfect.
I prefer a generic way to do it.
I have this extension for the UINavigationController :
extension UINavigationController {
func backToViewController(vc: Any) {
// iterate to find the type of vc
for element in viewControllers as Array {
if "\(element.dynamicType).Type" == "\(vc.dynamicType)" {
self.popToViewController(element, animated: true)
break
}
}
}
}
Let's say I have a FOHomeVC class (who is a UIViewController) instantiated in the navigation stack.
So I would do this in my code:
self.navigationController?.backToViewController(FOHomeVC.self)
I have added an extension to UINavigationController which helps you to find if that controller exist in navigation stack. If yes then it will be popped to that controller or else you pass new controller to push with pushController param.
extension UINavigationController {
func containsViewController(ofKind kind: AnyClass) -> Bool {
return self.viewControllers.contains(where: { $0.isKind(of: kind) })
}
func popPushToVC(ofKind kind: AnyClass, pushController: UIViewController) {
if containsViewController(ofKind: kind) {
for controller in self.viewControllers {
if controller.isKind(of: kind) {
popToViewController(controller, animated: true)
break
}
}
} else {
pushViewController(pushController, animated: true)
}
}
}
Swift 4 / Swift 5
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: HomeViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
I prefer a "real generic" and more functional approach.
So I came up with following UINavigationController extension functions. You can also use the first function, for anything else, where you just need to access a specific VC in the navigation stack.
Extensions
extension UINavigationController {
func getViewController<T: UIViewController>(of type: T.Type) -> UIViewController? {
return self.viewControllers.first(where: { $0 is T })
}
func popToViewController<T: UIViewController>(of type: T.Type, animated: Bool) {
guard let viewController = self.getViewController(of: type) else { return }
self.popToViewController(viewController, animated: animated)
}
}
Usage
self.navigationController?.popToViewController(of: YourViewController.self, animated: true)
This should work at least in Swift 4 and 5.
Find your view controller from navigation stack and pop to that view controller if it exists
for vc in self.navigationController!.viewControllers {
if let myViewCont = vc as? VCName
{
self.navigationController?.popToViewController(myViewCont, animated: true)
}
}
swift5
let controllers : Array = self.navigationController!.viewControllers
self.navigationController!.popToViewController(controllers[1], animated: true)
Swift 5 Answer of #PabloR is Here :
extension UINavigationController {
func backToViewController(vc: Any) {
// iterate to find the type of vc
for element in viewControllers as Array {
if "\(type(of: element)).Type" == "\(type(of: vc))" {
self.popToViewController(element, animated: true)
break
}
}
}
}
Usage :
self.navigationController?.backToViewController(vc: TaskListViewController.self)
In latest swift
#IBAction func popToConversationsVC(_ sender: UIButton) {
if (self.navigationController != nil) {
for vc in self.navigationController!.viewControllers {
if vc is ConversationsVC {
self.navigationController?.popToViewController(vc, animated: false)
}
}
}
}
For Swift 4.0 and above Using Filter
guard let VC = self.navigationController?.viewControllers.filter({$0.isKind(of: YourViewController.self)}).first else {return}
self.navigationController?.popToViewController(VC, animated: true)
Please use this below code for Swift 3.0:
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
for aViewController:UIViewController in viewControllers {
if aViewController.isKind(of: YourViewController.self) {
_ = self.navigationController?.popToViewController(aViewController, animated: true)
}
}
I needed to use this, because in some cases app crashes:
if let navVC = self.navigationController {
let views = navVC.viewControllers as Array
for controller in views {
if controller.isKind(of: YourVC.self) {
navVC.popToViewController(controller, animated: true)
return
}
}
}
This solution worked for me :)
extension UINavigationController {
func backToViewController(_ viewController: AnyClass, animated: Bool) {
guard let viewController = self.viewControllers.first(where: {$0.isKind(of: viewController)}) else { return }
self.popToViewController(viewController, animated: animated)
}
}
I adapt from all answer above. It look like Yakup Ad answer, because it's very short way.
I force type by using generic for argument, that make sure you must pass only UIViewController to this func.
I search viewController that already in stack by using .first this make me got only one VC then stop the loop.
I also return passing VC back if you need to customize somethings.
Let's enjoy.
extension UINavigationController {
func popToViewController<T: UIViewController>(_ viewController: T.Type, animated: Bool) -> T? {
guard let viewController = self.viewControllers.first(where: {$0 is T}) else { return nil }
self.popToViewController(viewController, animated: animated)
return viewController as? T
}
}
Usage
let poppedVC = self.navigationController?.popToViewController(HomeViewController.self, animated: true)
extension UINavigationController {
func popBack(to vc: AnyClass, animated: Bool = true) {
guard let elementFound = (viewControllers.filter { $0.isKind(of: vc) }).first else {
fatalError("cannot pop back to \(vc) as it is not in the view hierarchy")
}
self.popToViewController(elementFound, animated: animated)
}
}
simple and best solution without force unwrapped is
if let vc = navigationController.viewControllers.filter({$0 is YourViewController}).first as? YourViewController {
self.navigationController.popToViewController(vc, animated: true)
}

Resources