How to show an alert in Swift UIAlertView not working
You should use UIAlertController
UIAlertView
is deprecated.
You can show an alert with this code:
var alert = UIAlertController(title: "Alert", message: "test", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
UIAlertView is Not Working in Swift
From Xcode 6.0 UIAlertView class:
UIAlertView is deprecated. Use UIAlertController with a preferredStyle
of UIAlertControllerStyleAlert instead.
On swift ( iOS 8 and OS X 10.10 ), you can do this:
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
println("User click Ok button")
}))
self.presentViewController(alert, animated: true, completion: nil)
func handleCancel(alertView: UIAlertAction!)
{
println("User click cancel button")
}
If you want to use in 'ActionSheet' instead 'Alert' you need only to change the UIAlertControllerStyle for example:
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet)
UIAlertController won't display - In Swift
The problem is that by the time you call this following function:
present(alertController, animated: true, completion: nil)
your mail view controller is still visible. You have to make sure that the alertController
is presented on the top of window hierarchy. In your example, you have the following window hierarchy:
+---------------------------+
| MailComposeViewController |
+---------------------------+
||
+---------------------------+
| ContactUsViewController |
+---------------------------+
What you should do instead is to dismiss the email view controller first. When that is done, show you alert view controller.
let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
controller.dismissViewControllerAnimated(true){ () -> Void in
self.present(alertController, animated: true, completion: nil)
}
Alternatively, you can also present your alertController
on top of the MailComposeViewController
, like so:
let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
controller.present(alertController, animated: true, completion: nil)
How would I create a UIAlertView in Swift?
From the UIAlertView
class:
// UIAlertView is deprecated. Use UIAlertController with a
preferredStyle of UIAlertControllerStyleAlert instead
On iOS 8, you can do this:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Now UIAlertController
is a single class for creating and interacting with what we knew as UIAlertView
s and UIActionSheet
s on iOS 8.
Edit: To handle actions:
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
switch action.style{
case .Default:
print("default")
case .Cancel:
print("cancel")
case .Destructive:
print("destructive")
}
}}))
Edit for Swift 3:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
Edit for Swift 4.x:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
UIAlertView delay or not showing
Try with this
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
alert = [[UIAlertView alloc]initWithTitle:@""
message:[NSString stringWithFormat: @"Reload data ?"]
delegate:self
cancelButtonTitle:@"Yes"
otherButtonTitles:@"Cancel", nil];
[alert show];
NSLog(@"executed");
}];
Pop back to previous ViewController not working on Alert Error Message
Do like this,
let alert = UIAlertController (title: "Error message", message: errorMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler:{ (alertOKAction) in
self.popThisView()
}))
self.present(alert, animated: true, completion: nil)
SVProgressHUD.dismiss()
UIAlert view doesnt show up in Swift ?
It will not show up in viewDidLoad()
. You call the function from viewDidAppear()
How to present UIAlertController when not in a view controller?
I posted a similar question a couple months ago and think I've finally solved the problem. Follow the link at the bottom of my post if you just want to see the code.
The solution is to use an additional UIWindow.
When you want to display your UIAlertController:
- Make your window the key and visible window (
window.makeKeyAndVisible()
) - Just use a plain UIViewController instance as the rootViewController of the new window. (
window.rootViewController = UIViewController()
) - Present your UIAlertController on your window's rootViewController
A couple things to note:
- Your UIWindow must be strongly referenced. If it's not strongly referenced it will never appear (because it is released). I recommend using a property, but I've also had success with an associated object.
- To ensure that the window appears above everything else (including system UIAlertControllers), I set the windowLevel. (
window.windowLevel = UIWindowLevelAlert + 1
)
Lastly, I have a completed implementation if you just want to look at that.
https://github.com/dbettermann/DBAlertController
Error showing a UIAlertView in swift
Swift 5
You should do it this way:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Button", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
Related Topics
How to Import Zbar Framework in Swift Project
Swift Execute Command Line Command in Sandbox Mode
Trouble Passing Array Through Prepareforsegue
How to Use Protocols for Stucts to Emulate Classes Inheritance
Showing Action Sheet in The Custom Cell in Swift
Higlight Speech Utterance in Swiftui
How to Decorate Siesta Request with an Asynchronous Task
How to Show an Alert in Swift UIalertview Not Working
Firebase - Swift 4: Message Sent But Not Received
Swiftui Tabview with List Not Refreshing After Objected Deleted From/Added to Core Data
How to Detect Encoding in Data Based on a String
Qlpreviewcontroller Showing File Then Going Blank in Swiftui
Connecting Avaudiosourcenode to Avaudiosinknode Does Not Work
Having Trouble with Musickit Sample App Provided by Apple
External Library Usage in Xcode
How to Remove Items from an Array When Deselecting a Row in a UItableview