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.
If I understand your real real question, it is that you want to use
numberOfVehilce
inviewDidAppear
, 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 'viewDidAppear
that is visible to the
UIKit` 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 inviewDidAppear
.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 sinceviewDidAppear
will have finished executing before the delegate method is called. You probably need to perform the segue in your delegate function.