Error404 Error404 - 3 months ago 9
iOS Question

How can I call a function of UIViewController from its reference on a different swift file?

I have got my current

visibleViewController
on a swift file:

let currentController = self.navigationController?.visibleViewController


and I have a function on the swift file associated to the actual
UIViewController
called
chargeData
.

What I am trying to do is to call that function from the first file but I am not able to do it. I tried doing:

currentController.chargeData()


but an error is shown:


Value of type 'UIViewController?' has no member 'chargeData'


I can guess from the error log that of course it is not recognizing the swift file in which the actual
UIViewController
is associated.

So what should I do to access to the function of my actual
UIViewController
from its reference?

Thanks in advance!

Answer

You have to typecast your currentController variable in order to call any method you created on your own.

let currentController = self.navigationController?.visibleViewController as? YourViewController //Here YourViewController is the name of the UIViewController subclass you created

Hope this helps.