UIAlertController not showing - ios

I'm trying to display a message to my users with alert controller but it wont show
let alert = UIAlertController(title: "Error", message: "Incorrect username/password", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "okay", style: UIAlertActionStyle.Default, handler: nil))

looks like you haven't presented it yet. paste this code where you want to show the alert
self.present(alert, animated: true, completion: nil)

Related

how to alert view controller remove from window

I'm new to swift . I want to remove alert view from window. So my question is when alert view show it under the window .If so how can I remove alert view from window proper way .
let alert = UIAlertController(title: "Whoops", message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
if your need more information let me know please.
Thanks!

How to make alert view appear in center of the view controller using swift 2.2

I am developing a app using swift 2.2, in my app I am having a button when a user taps on that button alert view will appear which contains four buttons in vertical everything works fine but alert view appearing in bottom of the view controller how to display in center of the view controller.
Note:I don't want to display any title and message just i have to show four buttons only.
If I use this one:
let alert = UIAlertController(title: " ", message: "select colors", preferredStyle: UIAlertControllerStyle.Alert)
It helps to display in center of the view controller but without title and message it looks not better so I used following code
Code:
let alert = UIAlertController()
alert.addAction(UIAlertAction(title: "Red Color", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Blue Color", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Green Color", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Yellow Color", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Just set the title and the message to nil. I tested it in swift 3.
let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.alert)
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.present(alert, animated: true, completion: nil)
It will come automatically in center for its Property :) but what you actually wanted?
Add below lines in your code before you present your alert,
alert.popoverPresentationController!.sourceView = self.view
alert.popoverPresentationController!.sourceRect = self.view.bounds

UIAlertView Crashes App

I'm trying to make a UIAlertView on my Parse app but for some reason every time I run it, it crashes and I'm taken to ApplicationDelegate where I get a SIGABRT. Here is my alert code, I'm pretty sure I'm not doing anything wrong because it's worked before... Is it because I'm loading data into a tableview from Parse?
func displayAlert(title: String, message: String) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
}))
presentViewController(alert, animated: true, completion: nil)
}
Not sure what your issue is. You could simplify the code as follows (Xcode 7b6) by eliminating the empty closure and shortening the arguments when possible. But I don't think this is your problem. Instead, you should show the code where you call this function.
var alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)

Show alert in AppDelegate in Swift [duplicate]

This question already has answers here:
How to show UIAlertController from Appdelegate
(6 answers)
Closed 4 years ago.
I try the next code snippet:
var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
in my AppDelegate, but it prints me the next error in console:
Warning: Attempt to present <UIAlertController: 0x7ff6cd827a30> on <Messenger.WelcomeController: 0x7ff6cb51c940> whose view is not in the window hierarchy!
How can I fix this error?
This is what i'm using now to do that.
var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)
var okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Swift 5:
let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertController.Style.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
// show the alert
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
As per Jorge's answer, updated for Swift 4
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
Swift 3.0 or above, Working in all condition , like in case of tab bar, in case of presented view etc ..
let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
// show alert
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
I had the similar problem.
I have fixed it by presenting UIAlertController in Main Queue.
Code Looks like following.
let alert = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)
let actionYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
print("action yes handler")
})
let actionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
print("action cancel handler")
})
alert.addAction(actionYes)
alert.addAction(actionCancel)
DispatchQueue.main.async {
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
Have you tried using UIApplication.shared.keyWindow?.rootViewController?.present(...) ?
I suppose you are calling that code snippet from the applicationDidFinishLunchingWithOptions.
I tried it as a matter of fact because I had to. The thing is: what you are trying to do is correct but the ViewController that the AppDelegate makes and presents is about to be put on screen and before that, the code snippet tries to create an alertView and put in on top of non existent View of the RootViewController.
What I would do is move it to another delegate call which is guaranteed to be called after the RootViewController is presented.
func applicationDidBecomeActive(application: UIApplication) {
//This method is called when the rootViewController is set and the view.
// And the View controller is ready to get touches or events.
var alert = UIAlertController(title: "Alert", message: "Cannot connect to :", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
But as always know the responsibility of the AppDelegate. It is to handle the application lifecycle and application wide delegate calls and events. If putting code here makes sense, then do it. But if you will be better off putting the code on the rootViewController or other parts then think about it too.
Anyway, hope it helps. Cheers!
I would suggest NOT doing this in the AppDelegate. The App Delegate it intended to handle Delegate functions from the OS rather than implementing things like alert views.
If you are wanting to present an alert view here to be shown at the start of the app I would do this by implementing it in your first view controller.

Alertview when there is no profile picture added (Swift)

Im new to programming and need some help here I have created a signup page in swift with some text fields and buttons for names, passwords. I have added a UIImage for users to add their picture. I want each users to MUST include a picture to be able to signup.
if (user does not include picture empty) <--- code here {
var alert = UIAlertController(title: "Warning", message: "you need to add profile picture", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"ok", style:UIAlertActionStyle.Default, handler:nil))
self.presentViewController(alert, animated: true, completion: nil)
}
thank you.
You can check this way if imageView is nil or not:
if yourImageView.image == nil {
var alert = UIAlertController(title: "Warning", message: "you need to add profile picture", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}

Resources