Compare two viewControllers swift

check if viewcontroller is in navigation stack swift
get top view controller swift
get current navigation controller swift
check if viewcontroller is top
xctest get current viewcontroller
get current view controller from tab bar swift
check view controller type

I need to compare if two views are the same, I am getting the views like this

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
        let newController = self.storyboard!.instantiateViewController(withIdentifier: "Reservar")

when I print the value of the variables I get this

<Resto.ReservaViewController: 0x7fc9f0e4a860>
<Resto.ReservaViewController: 0x7fc9f0d074a0>

They are the same ViewController, but that number at the end is different, so I think that is why the if (viewController == newController) is false

With your code:

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
let newController = self.storyboard!.instantiateViewController(withIdentifier: "Reservar")

You can be completely certain that the two view controllers are not the same object. They may or may not be the same type of view controller (the same class) but the function instantiateViewController() always creates a brand-new, never-existed-before-this-moment, unique instance of the view controller. It might be an identical twin to another instance, with all the same settings, but it is still a unique object.

Tell us more about what you are trying to do. Are you looking to see if the object from the navigation controller is the type you are expecting?

Then you could use code like this:

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
if viewController is ReservaViewController {
   //code to operate on that type
} else {
   //Code for other types of view controller
}

Or

if let viewController = navController.viewControllers[navController.viewControllers.count - 2] as? ReservaViewController {
  //Code to operate on a ReservaViewController
} else {
  //Code to deal with a view controller that's NOT a ReservaViewController
}
EDIT:

The expression navController.viewControllers[navController.viewControllers.count - 2] is dangerous without range checking. If the navigation controller only contains 1 view controller, it will crash with an index out of range error.

How to compare UIViewController in Swift 3?, If you want to compare to a particular view controller you have to compare their refererences. Try this if(vc === viewController) ) { return (self. In short, you’ll need two things to connect a UI element to your code: Outlet Property: The outlet property is the reference for the UI element in your view controller class. You code it with Swift, in Xcode. Outlet Connection: The outlet connection is what binds a UI element to the outlet property.

Two chairs are both chairs but they are still not the same chair.

It's the same for classes and objects.

Let's say your view controller is an instance of the ViewController class. Then what you want to know is whether viewController is ViewController. Just like asking "is this a chair?"

I have two UIViewControllers that I need to compare to see if they , In Swift 3 you can compare object's types with type(of:) method: class VC1: UIViewController { } class VC2: UIViewController { } let vc1 = VC1()  extension UINavigationController { func popTo(controllerToPop:UIViewController) { //1. get all View Controllers from Navigation Controller let controllersArray = self.viewControllers //2. check whether that view controller is exist in the Navigation Controller let objContain: Bool = controllersArray.contains(where: { $0 == controllerToPop

Do you want to compare the Type of the two view controllers?

print(type(of: viewController) == type(of: newController))

How do you check current view controller class in Swift?, childViewControllers { for viewController in viewControllers { if viewController.​isKindOfClass(MyViewControllerClass) { println("Found it!!!")  Total noob here to Swift 3. All I need to do is pass a double value from one ViewController to another via the 2 user input text fields. I've tried numerous solutions and have read everything I can find on passing data between ViewControllers. I get a 'fatal error: unexpectedly found nil while unwrapping an Optional value'.

View Controllers: Ultimate Guide For iOS & Swift – LearnAppMaking, The Swift code that we use to bring a User Interface to life belongs in the view controller. But the view and the controller are also two distinct  You can pass data between view controllers in 6 ways: By using an instance property (A → B) By using segues (for Storyboards) By using instance properties and functions (A ← B) By using the Delegation pattern. By using a closure or completion handler. By using NotificationCenter and the Observer pattern.

How to test which class presented a UIViewController, Consider the following two example. If you present a UIViewController via a modal segue you can test which class the presenting view  If you have a value in one view controller and want to pass it to another, there are two approaches: for passing data forward you should communicate using properties, and for passing data backwards you can either use a delegate or a block. Passing data forward is used when you want to show some information in a detail view controller.

How to pass data between two view controllers, If you have a value in one view controller and want to pass it to another, there are two approaches: for passing data forward you should  To configure the title of our second view controller manually, open up the corresponding Swift class. UINavigationController sets the title of the top navigation bar to be the title of the current view controller.  Instead of setting the title of the navigation controller directly, we can set the title of the second activity in viewDidLoad.