UIAlertView deprecated? - swift

I want to use an alert box with a "Ok" button to return to the previous view controller.
I use this code :
let alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert.show()
But Xcode says that it is deprecated, so I've tried this :
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
but the Alert Box won't display...
Any ideas?

If you use swift 3 you can try this:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: nil))
self.present(alert, animated: true, completion: nil)
Check here for more detail.

Related

How to create a drop down menu

How can I create a menu like this using swift?
Example
It's alertController with actionSheet. User this method in your button action for example.
func handleAlert()
let alertController = UIAlertController(title: "Change Profile Photo", message: nil, preferredStyle: .actionSheet)
//First action
alertController.addAction(UIAlertAction(title: "Remove current photo", style: .destructive, handler: { (_) in
// Your remove photo code here
}))
alertController.addAction(UIAlertAction(title: "Import From Facebook", style: .default, handler: { (_) in
// Your import from facebook code here
}))
//Cancel action
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}

Adding TextField to AlertController breaks AlertController

I am currently trying to open up an AlertController with a TextField inside. When running
let configAlert = UIAlertController(title: "Configure Add-On", message: "Enter Your Add-On Name:", preferredStyle: UIAlertControllerStyle.alert)
configAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in
// Handle Input
}))
present(configAlert, animated: true, completion: nil)
everything works fine, but as soon as I add the TextField
configAlert.addTextField { (textField) in
textField.placeholder = "Name"
}
the Alert takes about 10 times longer to open, instantly dismisses, and I get this error in the console spammed about 30 times:
2017-11-26 13:04:08.985783-0500 MinelyMod[380:14792] Warning: Attempt to dismiss from view controller <UISplitViewController: 0x147e0a6a0> while a presentation or dismiss is in progress!
Here is the completed AlertController thats failing
let configAlert = UIAlertController(title: "Configure Add-On", message: "Enter Your Add-On Name:", preferredStyle: UIAlertControllerStyle.alert)
configAlert.addTextField { (textField) in
textField.placeholder = "Name"
}
configAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in
// Handle Input
}))
present(configAlert, animated: true, completion: nil)
let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in
let textField = alertController.textFields![0] as UITextField
// do something with textField
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
textField.placeholder = "Search"
})
self.present(alertController, animated: true, completion: nil)

Why doesn't my alert view controller work

let alert = UIAlertController(title: "Title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Title", style: UIAlertActionStyle.default, handler: { action in self.alertFunc() }))
If I build this the alert view doesn't appear. What have I missed?
P.S. I know there are some similar question but to find out what they have and I have missed is hard
You have to present the alerte view on your view.
self.present(alert, animated: true, completion: nil)
You need to present it too on your current context:
self.present(alert, animated: true, completion: nil)
Add that row at the end of your alert declaration:
let alert = UIAlertController(title: "Title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Title", style: UIAlertActionStyle.default, handler: { action in
self.alertFunc()
}))
self.present(alert, animated: true, completion: nil)

How to Call a Function when Ok is pressed in an UIAlert

Im try to simply call a function called "GoToNewShoot" When the user press the "ok" button when the Alert pops up Thanks!.
Code:
#IBAction func GobacktoCamera(sender: AnyObject) {
var alertController = UIAlertController(title: "Just Checking", message: "Are You Sure You Want to Start over ", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
You can do it by using handler from addAction this way:
#IBAction func GobacktoCamera(sender: AnyObject) {
var alertController = UIAlertController(title: "Just Checking", message: "Are You Sure You Want to Start over", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { action in
//run your function here
self.GoToNewShoot()
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
func GoToNewShoot(){
println("Method Called")
}
alerty.addAction(UIAlertAction(title: "Just Checking",
style: UIAlertActionStyle.Default,
handler: {(alert: UIAlertAction!) in print("Are You Sure You Want to Start over")}))

Error showing a UIAlertView in swift

Im trying to show a UIAlertView in my swift App
alert = UIAlertView(title: "",
message: "bla",
delegate: self,
cancelButtonTitle: "OK")
alert!.show()
=> BAD_ACESS error in:
-[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()
so I suspected self. I set it to nil
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
=> ARM_DA_ALIGN error in:
-[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()
the full code
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIAlertViewDelegate {
#lazy var window = UIWindow(frame: UIScreen.mainScreen().bounds)
#lazy var locationManager = CLLocationManager()
var alert: UIAlertView? = nil
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//setup dummy window
self.window.backgroundColor = UIColor.whiteColor()
self.window.rootViewController = UIViewController()
self.window.makeKeyAndVisible()
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
return true
}
}
How to do it right? :)
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)
Even though UIAlertView is depreciated in iOS8, you can get away with using it but not through it's init function. For example:
var alert = UIAlertView()
alert.title = "Title"
alert.message = "message"
alert.show()
Atleast this is the only way so far I've been able to successfully use an UIAlertView. I'm unsure on how safe this is though.
This is what I have used to make an alert popup in swift.
let myAlert = UIAlertView(title: "Invalid Login",
message: "Please enter valid user name",
delegate: nil, cancelButtonTitle: "Try Again")
myAlert.show()
var 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)
To handle actions:
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: {
action in switch action.style {
case .Default:
println("default")
case .Cancel:
println("cancel")
case .Destructive:
println("destructive")
}
}))
I was successfully able to show a UIAlertView using this code:
var alert = UIAlertView()
alert.title = "Title"
alert.message = "Message"
alert.addButtonWithTitle("Understood")
alert.show()
The code "alert.addButtonWithTitle("Understood")"
adds a button for the user to push after they have read the error message.
Hope this helps you.
Xcode 10, Swift 4.2 version
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)