trigger viewDidAppear in extension or how to use variable from extension in viewController?

501 views Asked by At

Hi I want to put the viewDidAppear in my extension, but it cannot be triggered...

see the code:

extension MyAutoWelcomViewController: VehicleMangerDelegate {
    
    
    func didUpdateRdwInfo(rdwData: RdwModel) {
        
    }
    
    func didUpdateVehicleInfo(vehicle: VehicleModel) {
        DispatchQueue.main.async {
            // code to pass data to a label
            print(vehicle.numberOfVehicles)
            
            let numberOfVehilce = vehicle.numberOfVehicles
            self.TestLabel.text = String(numberOfVehilce)

            func viewDidAppear(_ animated: Bool) {
                
                self.vehicleManager.delegate = self
                
                // check if vehicle list is nil
                
                self.vehicleManager.getVehicleData() // call api to get the user vehicle list
                
                if numberOfVehilce == 0 {
                    // show the welcome screen
                    print("vehicleList is nil")
                    
                } else {
                    // use segue to navigate to the auto list screen
                    self.performSegue(withIdentifier: Constants.segueMyAutoWelcomeToAutoList, sender: self)
                    print("vehicleList is not nil")
                }
                
            }
            
            
        }
    }

    func didFailWithError(error: Error) {
        print(error)
    }

}

The reason that I want to do this is because I have used a delegate (VehicleMangerDelegate) to pass some data from another screen to here, in the didUpdateVehicleInfo()function.

Can you help me on this? I need to trigger the performSegue based on the data from an API call (i.e., numberOfVehicle), and the data is passed to this screen using a delegate.

2

There are 2 answers

1
Paulw11 On BEST ANSWER

If I understand your real real question, it is that you want to use numberOfVehilce in viewDidAppear, but you want to declare that variable in your extension.

The problem with this is that extensions cannot add stored properties to the classes they extend; if you tried to declare that variable as a property outside your delegate function you would have gotten an error from the compiler.

To work around this you made it a local variable in the function.

Now your problem is that your viewDidAppear function can't see that variable as it is out of scope. You then created 'viewDidAppear` as an internal function so that it could see the variable.

This won't work for you though since you aren't declaring the view controller's 'viewDidAppearthat is visible to theUIKit` framework, but rather an internal function to your delegate function that just happens to have the same name.

You will need to declare numberOfVehicle as a property in your view controller itself, not in the extension. Then you can refer to it both in your delegate method and in viewDidAppear.

class MyAutoWelcomViewController: UIViewController {

    var numberOfVehicle = 0

    func viewDidAppear(_ animated: Bool) {
                
                self.vehicleManager.delegate = self
                
                // check if vehicle list is nil
                
                self.vehicleManager.getVehicleData() // call api to get the user vehicle list
                
                if numberOfVehilce == 0 {
                    // show the welcome screen
                    print("vehicleList is nil")
                    
                } else {
                    // use segue to navigate to the auto list screen
                    self.performSegue(withIdentifier: Constants.segueMyAutoWelcomeToAutoList, sender: self)
                    print("vehicleList is not nil")
                }
                
            }
    }

}

    extension MyAutoWelcomViewController: VehicleMangerDelegate {
    
    
    func didUpdateRdwInfo(rdwData: RdwModel) {
        
    }
    
    func didUpdateVehicleInfo(vehicle: VehicleModel) {
        DispatchQueue.main.async {
            // code to pass data to a label
            print(vehicle.numberOfVehicles)
            
            self.numberOfVehilce = vehicle.numberOfVehicles
            self.TestLabel.text = String(numberOfVehilce)
    }

I still have a concern however; you have not shown what getVehicleData does, but it refers to an API. If it executes asynchronously to fetch data and calls the delegate, then your code will not work as intended since viewDidAppear will have finished executing before the delegate method is called. You probably need to perform the segue in your delegate function.

3
Prettygeek On

How It should work? You are defining local function inside block of code, it is not viewDidApear from VC. You shouldn't trigger view controller lifecycle methods by your own, system does it if needed. You have to rethink Your code. If You are fresh to iOS development, find Stanford course on Youtube.