Error404 Error404 - 11 months ago 63
iOS Question

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

I have got my current

on a swift file:

let currentController = self.navigationController?.visibleViewController

and I have a function on the swift file associated to the actual

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:


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
is associated.

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

Thanks in advance!

Answer Source

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.