I have been working to create a UIAlertView in Swift, but for some reason I can't get the statement right because I'm getting this error:
Could not find an overload for 'init' that accepts the supplied arguments
Here is how I have it written:
let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: nil)
Then to call it I'm using:
button2Alert.show()
As of right now it is crashing and I just can't seem to get the syntax right.
// Generic Class For UIAlertView
Use:-
Use this code to display an alertview
Reference: Swift Show Alert using UIAlertController
For SWIFT4, I think, extending
UIViewController
and creating a reusable confirmation control is the most elegant way.You can extend the
UIViewController
as below:Then you can use it anytime:
I have made a singleton class to make this convenient to use from anywhere in your app: https://github.com/Swinny1989/Swift-Popups
You can then create a popup with multiple buttons like this:
or popups with a single button like this:
With the protocol extensions of Swift 2, you can make a protocol that provides a default implementation to your view controllers:
ShowsAlert.swift
ViewController.swift