Error executing the presentViewController command

392 views Asked by At

I am sure I am just missing one step but I am at a loss to figure out what. I am trying to write a function I can call repeatedly to error check several fields. When a required field is empty, the function will be called to remind the user and not allow him/her to proceed. I wrote the code to error trap the first field but then tried to make it a function I call for the rest. This is just for testing so I know the code works at least for one field (which it does).

When I run and the first field is empty, I get the error message and behavior I expect. When I run and the first field has data but the second field does not, I get no error message (no activity at all, actually).

I have tried to add self.presentViewController(dataErrorAlert, animated: true, completion: nil) in the function but it errors saying:

cannot invoke 'presentViewContoller' with an argument list of type'((String, error: String) -> (), animated: Bool, completion: nil)

I don't know what my options are for calling the presentViewController command. Here is the relevant code. Thank you for your assistance.

var missingDataError = ""

func dataErrorAlert(title:String, error:String) {
    var dataMissingAlert = UIAlertController(title: title, message: missingDataError, preferredStyle:UIAlertControllerStyle.Alert)
    dataMissingAlert.addAction((UIAlertAction(title: "OK", style: .Cancel, handler: nil)))
    self.presentViewController(dataErrorAlert, animated: true, completion: nil)
}

@IBAction func next(sender: AnyObject) {
    if enterPersonnelName.text == "" {
       let dataErrorAlert = UIAlertController(title: "Oops", message: "The Name field is empty and is required information.", preferredStyle:UIAlertControllerStyle.Alert)
        var cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
        dataErrorAlert.addAction(cancelAction)
        self.presentViewController(dataErrorAlert, animated: true, completion: nil)
        blankData = 1
    } else { if enterDrugID.text == "" {
            self.missingDataError = "The Drug ID field is empty and is required."
            self.dataErrorAlert("Ooops", error: missingDataError)
            blankData = 1
    } else { if enterPackageNumber.text == "" {
            self.missingDataError = "The Package Number field is empty and is required."
            self.dataErrorAlert("Ooops", error: missingDataError)
            blankData = 1
            }
    }
    }
        if blankData != 1 {
            //add code to pass data to next veiw controller
            //self.appIsWorking
            performSegueWithIdentifier("goToDispenseScreenThree", sender: self)
        }
    blankData = 0
}
1

There are 1 answers

2
Icaro On BEST ANSWER

It seems that dataErrorAlert is a function, I am guessing you want to display dataMissingAlert that is an UIAlertController, try this:

self.presentViewController(dataMissingAlert, animated: true, completion: nil)

You probably should try to rename your alert or your function as seems you have both with the name dataErrorAlert, I normally would user dataErrorAlertController. I hope that helps!