0

I'm trying to limit the show of code so I just want to call function containing two strings to create a uialert faster with 1 line instead of 5/

The error I'm getting

Use of unresolved identifier 'present'

at the line

present(alert, animated: true, completion: nil)

// Controlling Alerts for Errors
func showAlert(titleString: String, messageString: String) {

 // Alert to go to Settings
 let alert = UIAlertController(title: titleString, message: messageString, preferredStyle: .alert)

 alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: { _ in
     alert.dismiss(animated: true, completion: nil)
 }))

 self.present(alert, animated: true, completion: nil)
}
WokerHead
  • 947
  • 2
  • 15
  • 46

1 Answers1

1

In the comments, you explained that this is a stand-alone function. It should work if you make it an extension to UIViewController, for instance:

extension UIViewController {
    public func showAlert(_ title:String, _ message:String) {
        let alertVC = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert)
        let okAction = UIAlertAction(
            title: "OK",
            style: .cancel,
            handler: { action -> Void in
        })
        alertVC.addAction(okAction)
        present(
            alertVC,
            animated: true,
            completion: nil)
    }

}

And to call it in a UIViewController:

showAlert(
    "Could Not Send Email", 
    "Your device could not send e-mail.  Please check e-mail configuration and try again."
)
  • Thank you! and what if I also want to call it inside the system class where it was made as writing showAlert("", "") will give error `Use of unresolved identifier 'showAlert'` – WokerHead Aug 17 '17 at 17:49
  • anyway to call it outside of a ViewController? – WokerHead Aug 17 '17 at 18:00
  • Since `present` is a method within `UIViewController`, you needed to extend *that*. As written, as long as the *system class* as you call it is a subclass of `UIViewController`, you are good. I'm not sure exactly what it is you are trying, but it takes a view controller to present another one on top of it - I'm sure you know that as it's basic MVC. Second comment to follow.... –  Aug 17 '17 at 18:02
  • If what you are trying to do is to "present" something from outside a view controller, *maybe* you could use `NSNotification` to "tell" a view controller to present something. But remember, in iOS particularly (where there's no such thing as a "console app") you pretty much always have a view - which implies you have an active view controller. **That** is where you want to do any "presenting". Trying anything else? I'd recommend refactoring to this. –  Aug 17 '17 at 18:04
  • For example in my MainViewController I run code that gets from a swift class in system.swift (no viewcontroller) and within system.swift, I have a function that runs in the MainViewController but if the function in System has errors, display a UIAlert which ever viewcontroller is in – WokerHead Aug 17 '17 at 18:07
  • That *sounds* like sending a notification would do it. I've never tried it - but back in the OS X days you'd pass a message along a chain and the first interested "party" would handle it. –  Aug 17 '17 at 18:11