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.
The Old Way: UIAlertView
The New Way: UIAlertController
I got the following
UIAlertView
initialization code to compile without errors (I thing the last, varyadic part is tricky perhaps). But I had to make sure the class ofself
(which I am passing as the delegate) was adopting theUIAlertViewDelegate
protocol for the compile errors to go away:By the way, this is the error I was getting (as of Xcode 6.4):
As others mentioned, you should migrate to UIAlertController if you can target iOS 8.x+. To support iOS 7, use the code above (iOS 6 is not supported by Swift).
SWIFT 4 : Simply create a extension to UIViewController as follows:
Now in your ViewController, directly call above function as if they are provided by UIViewController.
on IOS 9, you can do this
In Swift 4.2 and Xcode 9.4.1
SIMPLE ALERT
ALERT WITH SHARED CLASS
If you want Shared class style(Write once use every where)
Now call alert like this in every ware
PRESENT ALERT TOP ALL WINDOWS
If you want to present alert on top of all views, use this code
Function calling
ALERT WITH TEXTFIELDS
If you want to add textfields to alert.