Swift - value of type ViewController does not have member * functionName *

In my application, I have several scripts, each of which shows a different one UIAlertController, so I created a function to display this warning, but I can’t name myself. Function inside "okAction". I get this error:

Type value 'ViewController'has no member'doAction'

Here is the code:

func showAlertController( titleOfAlert: String, messageOfAlert : String, doAction : () )
{
    let refreshAlert = UIAlertController(title: titleOfAlert, message: messageOfAlert, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default) {
        UIAlertAction in

        self.doAction()
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default) {
        UIAlertAction in
    }

    refreshAlert.addAction(okAction)
    refreshAlert.addAction(cancelAction)

    self.presentViewController(refreshAlert, animated: true, completion: nil)
}

Here is one of the functions that I call:

func changeLabel1()
{
    label.text = "FOR BUTTON 1"
}

How can I solve it?

+4
source share
3 answers
  • delete selfbefore doAction()since you are not calling an action on the self object.

  • , , Invalid use of '()' to call a value of non-function type '()'. , doAction , . . doAction () -> Void - Void, .. .

:

func showAlertController( titleOfAlert: String, messageOfAlert : String, doAction : () -> Void ) {
    ...
    let okAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default) { action in
        doAction()
    }
    ...
}

action doAction, (UIAlertAction) -> Void doAction(action).

+7

, :

func showAlertController( titleOfAlert: String, messageOfAlert : String, doAction : (() -> Void) ){

   // and then call it like that
   doAction()

}

, this.doAction(), , , , ?

0

doAction showAlertController.

-: doAction:() doAction:() → () , doAction Closure .

Secondly: the call should not be self.doAction () , but only doAction () because its parameter, not an instance variable

0
source

Source: https://habr.com/ru/post/1621779/


All Articles