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 not appearing
Make loginAlert a class variable. presentViewController does not keep a reference to the alertController. Because it is in a local variable now, it will be deleted at the end of viewDidAppear.
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 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");
}];
UIAlert view doesnt show up in Swift ?
It will not show up in viewDidLoad()
. You call the function from viewDidAppear()
Problems with getting text from UIAlertView textfield
You may go with UIAlertController instead of UIAlertView.
I've already implemented and tested too using UIAlertController for what you actually want. Please try the following code
var tField: UITextField!
func configurationTextField(textField: UITextField!)
{
print("generating the TextField")
textField.placeholder = "Enter an item"
tField = textField
}
func handleCancel(alertView: UIAlertAction!)
{
print("Cancelled !!")
}
var alert = UIAlertController(title: "Enter Input", message: "", preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler(configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler:{ (UIAlertAction) in
print("Done !!")
print("Item : \(self.tField.text)")
}))
self.presentViewController(alert, animated: true, completion: {
print("completion block")
})
Related Topics
iOS 7.0 and Arc: Uitableview Never Deallocated After Rows Animation
How Is Filters Uiscrollview/Uicollectionview in Apple's Photos App Implemented That It Opens So Fast
Xcode 5 - "No Application Records Were Found" When Trying to Validate an Archive
How to Change an iOS Device Volume Programmatically
How to Show Specific Language Keyboard When User Input Values in Uitextfield in iPhone App
Auto-Implement Swift Protocol Methods in Xcode
Collectionview Duplicate Cell When Loading More Data
Xcode 9 Simulator Error Unable to Boot the Simulator Launchd Failed to Respond MAC Os Sierra
Displaying Youtube Video Ads with Youtube Iframe
How to Play Multiple Audio Files Simultaneously
Code Signing Issue in Xcode Version 8
How to Use Nsurlsession to Determine If Resource Has Changed
Select Items Programmatically in Uicollectionview
Xcode Creates Wrong IPA Folder Structure
Uiactivityviewcontroller Completion Handler Returns Success When Tweet Has Failed
Determine Percentage of Visibility of Horizontal Collectionview Cells on Screen