Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare UIViewController in Swift 3?

I am trying to compare to UIViewController in Swift 3 but there is some error

extension UINavigationController
{
    func myPopToViewController(viewController:UIViewController, animated:Bool) -> UIViewController? {
        var arrViewControllers:[UIViewController] = []

        arrViewControllers = self.viewControllers
        for vc:UIViewController in arrViewControllers {
            if(vc.isKind(of: viewController) ) // This Line gives me error
            {
                return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
            }

        }
        return nil
    }

}

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30: Cannot convert value of type 'UIViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type')

and if try to use

if(vc is viewController)

It gives

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:22: Use of undeclared type 'viewController'

I am calling it through this

self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), animated: false)
like image 403
Varun Naharia Avatar asked Mar 06 '17 11:03

Varun Naharia


People also ask

How do I use UIView in SwiftUI?

Using UIView in SwiftUI. The process of integrating UIView is almost identical to the one of UIViewController. Namely, the SwiftUI view must conform to the UIViewRepresentable protocol and implement the same set of methods. Here is an example of how we can represent UIActivityIndicatorView in SwiftUI:

How to integrate a view controller into SwiftUI view hierarchy?

The process of integrating a view controller into SwiftUI view hierarchy is next: Declare a SwiftUI view that conforms to UIViewControllerRepresentable. Implement the two required methods makeUIViewController () and updateUIViewController (). Let’s get started and wrap a font picker into a SwiftUI view:

How to represent uiactivityindicatorview in SwiftUI?

Here is an example of how we can represent UIActivityIndicatorView in SwiftUI: Let’s display the spinner in ContentView: If you run this code, you’ll see the following result: Every SwiftUI view that represents a UIKit view or view controller undergoes following steps that conclude its lifecycle:

What is the use of update method in SwiftUI?

The update method allows us to keep UIViewController in sync with SwiftUI state updates. In this example, we leave it empty, as our view controller does not depend on the rest of our SwiftUI app for any data. Finally, in ContentView, show the font picker modal. In SwiftUI, we present modals with the sheet () view modifier.


1 Answers

for viewsController in arrViewControllers
{
    if(viewsController.isKind(of: YourControllerClassName.self)){
    }
}
like image 182
Punit Avatar answered Oct 17 '22 18:10

Punit